gpt4 book ai didi

Java I/O : How to Print Loop Outputs to one file?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:22 25 4
gpt4 key购买 nike

我试图将多个联系人写入并保存到一个文件中,然后将其存储为十六进制文件,但它一直在创建多个文件(每个联系人一个)。我试图移动:

  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 : How to Print Loop Outputs to one file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com