Java I / O:如何将循环输出打印到一个文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java I / O:如何将循环输出打印到一个文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2981字,纯文字阅读大概需要5分钟。
内容图文
我正在尝试将多个联系人写入并保存到一个文件中,然后将其存储为十六进制文件,但它会不断创建多个文件(每个联系人一个).我试图移动:
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
//append information to the string to be converted
utfString = name + "\r\n" + pNumber + "\r\n" + fact;
//outputs variables to file in hexadecimal format
PrintStream fileStream = new PrintStream(fName);
fileStream.println(toHex(utfString));
While循环中的“部分”部分,该部分将在用户说他们没有其他联系人要添加到文件之后创建文件.我要使用此代码的路径是,每个联系人都以十六进制代码形式写入文件,然后用户说他们想输入另一个联系人,因此程序在第一个联系人之后将新联系人以十六进制代码写入文件中.一.
码:
import java.util.Scanner;
import java.io.PrintStream;
import java.io.IOException;
public class Exercise27{
public static String toHex(String currString){
//converts a string to a byte-array
byte[] preConvert = currString.getBytes();
//converts byte-array to hexadecimal string using a function from the javax library included in Java Platform SE 7 and onwards
return javax.xml.bind.DatatypeConverter.printHexBinary(preConvert);
}
public static void main(String[] args) throws IOException{
//starts keyboard input
Scanner kbReader = new Scanner(System.in);
Boolean MoreContacts = true;
String More;
do{
//declare variable to convert to hex later on
String utfString = "";
//create variables for user-information
System.out.print("Type your full name, followed by hitting 'enter': ");
String name = kbReader.nextLine();
System.out.print("Type your phone number, followed by hitting 'enter': ");
String pNumber = kbReader.nextLine();
System.out.print("Type one interesting fact about yourself, followed by hitting 'enter': ");
String fact = kbReader.nextLine();
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
//append information to the string to be converted
utfString = name + "\r\n" + pNumber + "\r\n" + fact;
//outputs variables to file in hexadecimal format
PrintStream fileStream = new PrintStream(fName);
fileStream.println(toHex(utfString));
System.out.println("More contacts? (Enter y or n)");
MoreContacts = false;
More = kbReader.nextLine();
System.out.println("More: " + More);
if((More.equalsIgnoreCase("y")) || (More.equalsIgnoreCase("yes")))
{
MoreContacts = true;
}
}while(MoreContacts);
PrintStream fileStream;
//close your systems
//fileStream.close();
kbReader.close();
}
}
解决方法:
如果您想为联系人使用相同的文件,那么您实际上应该只要求输入一次文件名.在这种情况下,可能在while循环之前.
因此,将循环之前的行移动:
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
然后,我建议您使用FileWriter,然后可以选择附加到文件中.
File yourFile = new File(fName);
FileWriter fw = new FileWriter(yourFile, true);
然后在循环中,您可以使用
fw.write(toHex(utfString));
但这不会包含newlinw,因此您可能还需要添加
fw.write("\r\n");
然后,不要忘记在将所有数据写入到写入器之后将其关闭
fw.close();
内容总结
以上是互联网集市为您收集整理的Java I / O:如何将循环输出打印到一个文件?全部内容,希望文章能够帮你解决Java I / O:如何将循环输出打印到一个文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。