我正在尝试将多个联系人编写并保存到一个文件中,然后将其存储为Hex文件,但它会不断创建多个文件(每个联系人一个)。我试图移动:
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循环,它会在用户说没有更多联系人添加到文件后创建文件。我想用这段代码获取的路径是每个联系人都以Hex代码的形式写入文件,然后用户说他们想要输入另一个联系人,所以程序在第一个联系人之后立即将新联系人写入Hex文件之一。
代码:
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();
}
}
答案 0 :(得分:3)
如果您想为联系人使用相同的文件,那么您真的应该只询问一次文件名。可能在这种情况下,在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();
答案 1 :(得分:1)
问题是在循环内部创建了PrintStream
导致文件被截断,如PrintStream
构造函数javadoc中所述:
参数:
file - 用作此打印目标的文件 流。如果文件存在,那么它将被截断为零大小; 否则,将创建一个新文件。输出将被写入 该文件并被缓冲。
您可以尝试:
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);
System.out.print("Type a name for this file, followed by hitting 'enter': ");
String fName = kbReader.nextLine();
PrintStream fileStream = new PrintStream(fName);
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();
//append information to the string to be converted
utfString = name + "\r\n" + pNumber + "\r\n" + fact;
//outputs variables to file in hexadecimal
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();
}
}