使用 Java 将字符串数组写入文件 - 单独的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18871341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a string array to file using Java - separate lines
提问by user2791187
I'm writing a program that writes sets of observations in the form of a String array (from User input) to file. I am able to write an observation to a .txt file and then add a new observation without removing the previous data, but all my data is on the same line. I need each set of observations to be on a separate line.Additionally I will need to be able to access the file later on and read from it.
我正在编写一个程序,该程序以字符串数组(来自用户输入)的形式将观察集写入文件。我可以将观察写入 .txt 文件,然后在不删除以前数据的情况下添加新观察,但我的所有数据都在同一行上。我需要每组观察都在单独的行上。此外,我将需要能够稍后访问该文件并从中读取。
My code currently looks like this: (observation is the string array)
我的代码目前看起来是这样的:(观察的是字符串数组)
for(int i = 0; i < observation.length; i++) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("birdobservations.txt", true))) {
String s;
s = observation[i];
bw.write(s);
bw.flush();
} catch(IOException ex) {}
}
My output currently looks like this: CrowMOsloMay2015JayMOsloJune2012CrowMOsloMay2015RobinFBergenMay2012
我的输出目前看起来像这样: CrowMOsloMay2015JayMOsloJune2012CrowMOsloMay2015RobinFBergenMay2012
I would like it to look like this:
我希望它看起来像这样:
Crow M Oslo May2015
Jay M Oslo June2012
...etc
...等等
How do I do this? I assume I need some kind of loop, but I've been stuck on figuring this out for a while now.
我该怎么做呢?我想我需要某种循环,但我已经坚持解决这个问题有一段时间了。
采纳答案by Brian Agnew
Why not iterate within your try{}
block and use BufferedWriter.newLine()after each write ?
为什么不在try{}
块内迭代并在每次写入后使用BufferedWriter.newLine()?
If you need to be able to read the values back in later, you need to consider some unambiguous output format. Perhaps the simplest solution is a CSV format (I note your output data has spaces - you would need to separate your entries using something other than spaces in that case)
如果您需要能够稍后读回这些值,您需要考虑一些明确的输出格式。也许最简单的解决方案是 CSV 格式(我注意到您的输出数据有空格 - 在这种情况下,您需要使用空格以外的其他内容分隔条目)
回答by Viktor Ozerov
bw.write(s);
bw.write(System.getProperty("line.separator"));
bw.flush();
回答by loddn
bw.write(s);
bw.newLine();
bw.flush();
回答by Jabran Saeed
You need to push a line separator into the buffer.
您需要将行分隔符推入缓冲区。
newLine();
Here's the code
这是代码
for(int i = 0; i < observation.length; i++) {
try (BufferedWriter bw
= new BufferedWriter(new FileWriter("birdobservations.txt", true))) {
String s;
s = observation[i];
bw.write(s);
bw.newLine();
bw.flush();
} catch(IOException ex) {
ex.printStackTrace();
}
}
回答by Scadge
Sorry I can't comment Brian Agnew's answer because of my reputation, so I write it here.
抱歉,由于我的声誉,我无法评论Brian Agnew的回答,所以我写在这里。
Seems you never have any spaces in your array items, so you can successfully use them as a separators in your .txt file. When reading, just read the file line by line and separate items by split(" ")method.
似乎您的数组项中从来没有任何空格,因此您可以成功地将它们用作 .txt 文件中的分隔符。读取时,只需逐行读取文件并通过split(" ")方法分隔项目。