gpt4 book ai didi

java - 缓冲写入器在文本文件上写入内存垃圾

转载 作者:行者123 更新时间:2023-12-02 06:51:09 25 4
gpt4 key购买 nike

我的缓冲写入器正在我的 txt 文件中写入一些随机垃圾。我使用 int nodes = Integer.valueOf(NODES_TEXT_FIELD.getText()); 来存储一个只接受整数的 TextField 的值。

这是我的作家:

 private static void writeOnFile(BufferedWriter writer, int nodes){

try {
System.out.println(nodes);
System.out.println("Last check before write");
writer.write(nodes);
System.out.println(nodes);
} catch (IOException e) {

JOptionPane.showMessageDialog(null, "Failed to write data on file");
e.printStackTrace();
}
}

我的输出:

2
Last check before write
2

在文本文件中我发现:“?” (根据您输入的数字更改为另一个内存垃圾

有人知道可能出了什么问题吗?我在这里呆了 5 个小时到现在..

最佳答案

因为write()写一个字符。

Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Parameters:

c - int specifying a character to be written

您可以使用Writer#write(String) :

writer.write(String.valueOf(nodes));

关于java - 缓冲写入器在文本文件上写入内存垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016191/

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