java 如何在ArrayList java中添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27826417/
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
How to put a new line in an ArrayList java
提问by nnewbie
How to input a new line in an java String array. For example my input file is this:
如何在 java String 数组中输入新行。例如我的输入文件是这样的:
Student learns programming
java.Teacher rates
student.Nero gets ten.
and the output is in one line. Output:
并且输出在一行中。输出:
Student learns programming java.Teacher rates student.Nero gets ten.
my code used is this:
我使用的代码是这样的:
File f=new File(s3);
File _if=new File(s2);
File _dic=new File(s4);
f.createNewFile();
FileInputStream _inputfile=new FileInputStream(_if.getAbsolutePath());
BufferedReader _readfile=new BufferedReader(new InputStreamReader(_inputfile));
String _str=_readfile.readLine();
FileWriter _filewrite=new FileWriter(f);
BufferedWriter _buffwrt=new BufferedWriter(_filewrite);
int sum=0,sum2=0;
ArrayList<String> _array=new ArrayList<String>();
while (_str != null) {
String[] _vect = _str.split(" ");
int i;
for (i = 0; i < _vect.length; i++) {
_array.add(_vect[i]);}
_str =_readfile.readLine();
}
回答by josivan
You can use Apache Commons IO
您可以使用Apache Commons IO
Take a look in thismethod, that reads a file straightforward to a List.
看看这个方法,它直接将文件读取到列表。
回答by James C. Taylor IV
you can add a "\n" string to the arraylist after the for loop:
您可以在 for 循环后向数组列表中添加一个 "\n" 字符串:
for (i = 0; i < _vect.length; i++) {
_array.add(_vect[i]);
}
_array.add("\n");//this will add a new line character to the arraylist
回答by hfontanez
Instead of adding the newline character as a String
to the list, I would append the newline to the String itself:
String
我不会将换行符作为 a 添加到列表中,而是将换行符附加到字符串本身:
for (i = 0; i < _vect.length; i++) {
_array.add(_vect[i] + "\n");
}
I do not think wasting allocation of memory by making the newline String
a unique entry on the List
is a good idea.
我不认为通过将换行符设置String
为唯一条目来浪费内存分配List
是一个好主意。
回答by RealSkeptic
If the whole intention of this assignment is to read the file and write it back, you don't really need an ArrayList
at all. You can do something like:
如果此作业的全部目的是读取文件并将其写回,则您根本不需要 an ArrayList
。您可以执行以下操作:
while ( _str != null ) {
_buffwrt.write( _str );
_buffwrt.newLine();
}
This simply reads each line from the input file and writes it back to the output file.
这只是从输入文件中读取每一行并将其写回输出文件。
But I suspect, from your code, that you actually want to do something with the words in this file. You didn't explain what it is you are doing. Maybe you want to count them, or maybe you want to translate them to something.
但我怀疑,从您的代码中,您实际上想对这个文件中的单词做一些事情。你没有解释你在做什么。也许您想计算它们,或者您想将它们转化为某种东西。
The important thing, though, is that you don't have to keep all the words of the whole file in an ArrayList
together. You should do it for each line separately. You may even be able to work directly with the split array. For example, here is how you would count the words of the whole file, and still write each line separately.
但重要的是,您不必将整个文件的所有单词ArrayList
放在一起。您应该分别为每一行执行此操作。您甚至可以直接使用拆分数组。例如,这里是如何计算整个文件的字数,并且仍然单独编写每一行。
int sum = 0;
while ( _str != null ) {
String[] _vect = _str.split(" ");
sum += vect.length;
_buffwrt.write( _str );
_buffwrt.newLine();
}
This adds the number of words in each line to the total of words after reading each line. But it still writes the original line to the output file as before.
这会将每行中的单词数添加到阅读每行后的单词总数中。但它仍然像以前一样将原始行写入输出文件。
If what you want to do is make changes to the words and write them back changed, you could do something like:
如果您想做的是对单词进行更改并将其写回更改后的内容,您可以执行以下操作:
while ( _str != null ) {
String[] _vect = _str.split(" ");
for ( int i = 0 ; i < _vect.length; i++ ) {
// Change _vect[i] in some way
_vect[i] = ....;
// Write a space if it's not the first word
if ( i != 0 ) {
_buffwrt.write( ' ' );
}
_buffwrt.write(vect[i]);
}
_buffwrt.newLine();
}
What you do here is split the line, work on each word, and then write it to the output file. You also write a space before it if it's not the first word.
您在这里要做的是拆分行,处理每个单词,然后将其写入输出文件。如果它不是第一个单词,你也可以在它之前写一个空格。
The important point here is not to collect all the words in the file together, and then try to remember which word belonged to which line, or adding newlines to the ArrayList
, which are not words and may affect the logic of your program. Instead, work on each line separately, and write it separately, and then you can add a newline at the end of processing.
这里的重点是不要将文件中的所有单词收集在一起,然后尝试记住哪个单词属于哪一行,或者在 中添加换行符ArrayList
,这些不是单词并且可能会影响您程序的逻辑。相反,单独处理每一行,单独编写,然后您可以在处理结束时添加换行符。