在Java中将数组写入文件
您好,我已经查看了我能找到的所有其他与此类似的问题,但无法解决我的上一个问题,尽管其他问题的答案让我到目前为止。
我正在尝试将数组写入文件,到目前为止我可以做到这一点,但作者只是将所有内容写入同一行。由于某种原因,在记事本中查看时它不会接受我的新行命令(\n)。
尝试解释的示例:
数组内容: test、test2、test3、test4
写入文件
文件内容: test test2 test3 test4
而我希望文件为: 测试 测试2 测试3 test4
下面是我要写入文件的代码段。
public void save(String fileName) throws FileNotFoundException {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for ( int i = 0; i < nbrMovies; i++)
{
writer.write(movies[i].getName() + " \n");
}
writer.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
该文件不接受“\n”命令,而是仅在最后添加的电影旁边放置一个小框。
任何和所有的帮助将不胜感激。
Hi i've looked at all the other questions similar to this that I could find and haven't been able to fix my last problem, though the answers to other questions got me this far.
I'm trying to write an array to a file, and so far I can do that, but the writer just writes everything on the same line. For some reason it won't accept my new line command (\n) when viewed in notepad.
An example to try and explain:
Array contents: test, test2, test3, test4
Write to file
File contents: test test2 test3 test4
Whereas i want the file to be:
test
test2
test3
test4
Below is the segment of my code to write to the file.
public void save(String fileName) throws FileNotFoundException {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for ( int i = 0; i < nbrMovies; i++)
{
writer.write(movies[i].getName() + " \n");
}
writer.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
The file doesn't accept the "\n" command and instead just puts a little box next to the last movie added.
Any and all help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先,您不应该在每次写入后
close()
流;将close()
移到循环之外,最好移到finally
块中以确保调用它。其次,与其写“\n”,更好的做法是First of all, you shouldn't
close()
the stream after every write; move theclose()
outside your loop, preferably into afinally
block to ensure it is called. Second, rather than writing "\n", a better approach is将
writer.close();
从 for 循环中取出。你的代码应该是这样的:-Take
writer.close();
out of the for loop. Your code should be like this:-有这个 writer.newLine(); 并将调用移出循环以关闭。
There is this
writer.newLine();
and move the call to close out of the loop.用 PrintWriter 包装 FileWriter 或 BufferedWriter,您将可以访问 println(...) 方法。
Wrap the FileWriter or BufferedWriter with a PrintWriter, and you will have access to the println(...) method.
将 writer.close() 移到循环之外。另外,请确保在关闭 writer 之前将其刷新。
Move the
writer.close()
outside the loop. Also, make sure that you flush the writer before closing it.为什么不在循环外调用
close
呢?Why don't you call
close
outside the loop?我同意将 writer.close() 移到循环之外。但是有一种更简单的方法可以完成您想要的操作 - 无需调用换行函数,只需在语句末尾附加回车符而不是 return
即
更改语句:
writer.write(movies[i].getName() + " \n");
到: writer.write(movies[i].getName() + " \r\n");
I agree with moving the writer.close() outside the loop. But there is a simpler way of doing what you want - rather than calling the newline function, just append carriage return instead of return at the end of the statement
i.e.
change the statement:
writer.write(movies[i].getName() + " \n");
to:
writer.write(movies[i].getName() + " \r\n");