我必须读取一个文本文件并根据某些逻辑对文件进行一些更改。所以我使用
逐行读取文件 while ( (temp_string = inputstream.readLine()) != null )
{
/*after employing some other logic*/
outputstream.write(temp_string);
outputstream.flush();
}
其中 temp_string 是一个字符串,而 inputstream 的声明是:
BufferedReader inputstream = new BufferedReader(new FileReader(pathtosilentfile));
并且 outputstream
的声明是:
BufferedWriter outputstream = new BufferedWriter(new FileWriter(pathtooutputfile));
但是当我写入文本文件(新文件)时,有很多垃圾,在 Notepad++ 上显示为NULL。另外,写入的数据在记事本或写字板中看起来没问题。那么这是某种编码错误吗?我不明白!出了什么问题?
最佳答案
其一,您正在读取行而不是写入任何换行符。 temp_string
不包含任何换行符/换行符。尽管这似乎并不是您遇到的唯一问题。
关于java - 无法*正确*写入java文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232821/