gpt4 book ai didi

java - 我无法写入我创建的文本文件

转载 作者:行者123 更新时间:2023-11-30 05:51:08 25 4
gpt4 key购买 nike

我创建了一个带有一些标题的文本文件,然后将其关闭,之后当我有要写入该文件的数据时,什么也没有发生,我的代码在下面,当我尝试打印到它时得到的堆栈跟踪.

public class Writetofile {

BufferedWriter writer = null;

public void recordData(String record) throws IOException {

try {
writer.append(record);
writer.newLine();
writer.close();
} catch (Exception e) {e.printStackTrace();
}
}

public void createtxtfile(String[] details) throws IOException {
String machine = details[0];
String date = details[1];
String start_time = details[2];
try {
File new_cal = new File("C:\\Activity_Calibrator\\log\\" + machine + "//" + machine + ".txt");
new_cal.getParentFile().mkdirs();
FileWriter fwriter = new FileWriter(new_cal);
writer = new BufferedWriter(fwriter);
writer.append("Linear Calibration for " + machine + " carried out " + date);
writer.newLine();
writer.close();
} catch (Exception e) {
}
}
}

Message that is recieved
java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116)
at java.io.BufferedWriter.write(BufferedWriter.java:221)
at java.io.Writer.write(Writer.java:157)
at java.io.Writer.append(Writer.java:227)
at Writetofile.recordData(Writetofile.java:27)
at UserInterFace.update(UserInterFace.java:75)
at Comms.serialEvent(Comms.java:124)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

这很难理解,因为我读到我应该总是在写入 Stream 之后关闭它。

最佳答案

nothing happens, and I do not get an error

好的第一步是不要吞下异常——例如,您可以将堆栈跟踪打印到控制台:

} catch (Exception e) {
e.printStackTrace();
}

然后您将收到一条正确的异常消息,这应该可以帮助您找到问题。

关于java - 我无法写入我创建的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156905/

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