gpt4 book ai didi

java - 无法写入 java 中的文件,因为它正在使用 System.out.println 打印

转载 作者:行者123 更新时间:2023-12-01 20:52:21 30 4
gpt4 key购买 nike

File output=new File("C:\\\\MDU-1617-CSJ0098\\\\web\\\\products.txt");
BufferedWriter writer1 = new BufferedWriter(new FileWriter(output));
while(q_set2.next()) {
String s = String.valueOf(q_set2.getInt(1));
System.out.print(s);
writer1.write(s);
writer1.newLine();
}

输出是运行:123456 构建成功(总时间:0 秒)

但是文件中没有数据

最佳答案

This write 方法的作用是:

Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed.

因此,它写入缓冲区而不是直接写入文件。要使缓冲区flush,您需要调用flushclose方法,例如:

File output = new File("C:\\\\MDU-1617-CSJ0098\\\\web\\\\products.txt");
BufferedWriter writer1 = new BufferedWriter(new FileWriter(output));
while (q_set2.next()) {
String s = String.valueOf(q_set2.getInt(1));
System.out.print(s);
writer1.write(s);
writer1.newLine();
}
writer1.close();

close() 在内部调用 flush(),因此,在这种情况下您不需要显式调用 flush() (这是Javadoc)。

关于java - 无法写入 java 中的文件,因为它正在使用 System.out.println 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43027671/

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