gpt4 book ai didi

java - 将打印行和文本添加到 FileWriter (writer.write)

转载 作者:行者123 更新时间:2023-12-01 10:37:03 24 4
gpt4 key购买 nike

我想知道如何将打印行添加到文件写入器输出。当完整代码运行并打开 .txt 文件夹时,它看起来像这样

https://gyazo.com/b287d61eafb100dce1ce2476b71623e9

我想知道如何添加文本和打印行以使格式类似于:https://i.gyazo.com/def6e157e24dc0ed0fe68e1509152405.png

   try
{
FileWriter writer = new FileWriter("Gamer Report Data.txt");
writer.write(gamerName);
writer.write(System.getProperty( "line.separator"));
writer.write(gamerReport);
writer.close();
} catch (IOException e)
{
System.err.println("File does not exist!");
}

}

最佳答案

像这样:

 try
{
PrintWriter writer = new PrintWriter("Gamer Report Data.txt");
writer.println("Player: " + gamerName);
writer.println();
writer.println("-------------------------------");
//This line will split the gameReport string by the ':' separator
//Into an array of strings.
string[] report = gameReport.split(":");
//Edit here with regards to latest comment
//This will print the game score if the reports array has data.
if(report == null) {
writer.println("Game report was null.");
} else if(report.length == 3) {
writer.println("Game:" + report[0]+", score=" + report[1] +", minutes played="+ report[2]);
}
writer.close();
} catch (IOException e)
{
System.err.println("File does not exist!");
}

}

关于java - 将打印行和文本添加到 FileWriter (writer.write),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624445/

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