gpt4 book ai didi

Java IO : How to go to the next line after a certain amount of numbers have been written?

转载 作者:行者123 更新时间:2023-11-29 03:18:14 25 4
gpt4 key购买 nike

嗨,我想将一个数组写入一个文件,我可以这样做,除了我想在第一行写入数组的前 50 个对象,然后移动到下一行并写入另外 50 个数字,然后再写入下一行,依此类推上。

目前我有这个:

if (pause.save) {
Formatter f;
FileWriter fw;
PrintWriter pw = null;

File f2 = new File("docs/save.txt");

FileReader r;
BufferedReader r1;
try{


//f = new Formatter("docs/save.txt");

f2.createNewFile();

fw = new FileWriter(f2);
pw = new PrintWriter(fw);


pw.println(mapwidth);
pw.println(mapheight);

for(int i = 0; i < overview.s.toArray().length; i ++){
pw.println(overview.s.get(i).getID());
}



}catch(IOException e){
e.printStackTrace();
System.out.println("ERROR!!!");
}finally{

pw.close();
}
}

多年来我一直在尝试这样做,但无法弄清楚。

最佳答案

您可以通过以下方式执行此操作:-

for(int i = 0; i < overview.s.toArray().length; i ++){
pw.println(overview.s.get(i).getID());
if(i!=0&&i%50==0)
//newline here
}

关于Java IO : How to go to the next line after a certain amount of numbers have been written?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241335/

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