我想将一些数据写入文件,这是我的代码:
for (Season s : seasons) {
if (s.getYear() >= start && s.getYear() <= end) {
line += s.getYear() + ", " + s.getWinTeamName() + ", "
+ s.getMinorPremiersTeam() + ", "
+ s.getWoodenSpoonTeam()+ "\n"; }
}
System.out.println("File created.\n"); // Informing the user about
// the creation of the file.
out.write(line);
out.close();
我的文件已创建,但这是我得到的:
2005, Wests Tigers, Parramatta, Newcastle2006, Broncos, Storm, Rabbitohs
但我希望它是:
2005, Wests Tigers, Parramatta, Newcastle
2006, Broncos, Storm, Rabbitohs
我认为在行变量的末尾添加“\ n”会修复它,但它没有。
有关如何解决该问题的任何建议吗?
答案 0 :(得分:3)
您需要使用系统特定的换行符分隔符。见System.lineSeparator
答案 1 :(得分:1)
如果您使用java.io.BufferedWriter
,.newLine()
方法将插入相应的行分隔符。
for (Season s : seasons)
{
if (s.getYear() >= start && s.getYear() <= end)
{
String line = s.getYear() + ", " + s.getWinTeamName() + ", "
+ s.getMinorPremiersTeam() + ", "
+ s.getWoodenSpoonTeam();
out.write(line);
out.newLine();
}
}
答案 2 :(得分:0)
for (Season s : seasons) {
if (s.getYear() >= start && s.getYear() <= end) {
line += s.getYear() + ", " + s.getWinTeamName() + ", "
+ s.getMinorPremiersTeam() + ", "
+ s.getWoodenSpoonTeam()+ "\r\n"; }
}
System.out.println("File created.\n"); // Informing the user about
// the creation of the file.
out.write(line);
out.close();