Java 如何将字符串的 ArrayList 写入文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6548157/
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 write an ArrayList of Strings into a text file?
提问by Ricky
I want to write an ArrayList<String>
into a text file.
我想将一个ArrayList<String>
写入文本文件。
The ArrayList
is created with the code:
在ArrayList
与代码创建的:
ArrayList arr = new ArrayList();
StringTokenizer st = new StringTokenizer(
line, ":Mode set - Out of Service In Service");
while(st.hasMoreTokens()){
arr.add(st.nextToken());
}
采纳答案by Andrey Adamovich
import java.io.FileWriter;
...
FileWriter writer = new FileWriter("output.txt");
for(String str: arr) {
writer.write(str + System.lineSeparator());
}
writer.close();
回答by ribram
If you want to serialize the ArrayList object to a file so you can read it back in again later use ObjectOuputStream/ObjectInputStream writeObject()/readObject() since ArrayList implements Serializable. It's not clear to me from your question if you want to do this or just write each individual item. If so then Andrey's answer will do that.
如果您想将 ArrayList 对象序列化到一个文件,以便您可以稍后再次读回它,请使用 ObjectOuputStream/ObjectInputStream writeObject()/readObject(),因为 ArrayList 实现了Serializable。从你的问题中我不清楚你是想这样做还是只写每个单独的项目。如果是这样,那么安德烈的回答将做到这一点。
回答by stemm
You might use ArrayList overloaded method toString()
您可能会使用 ArrayList 重载方法 toString()
String tmp=arr.toString();
PrintWriter pw=new PrintWriter(new FileOutputStream(file));
pw.println(tmp.substring(1,tmp.length()-1));
回答by gabinetex
You can do that with a single line of code nowadays. Create the arrayList and the Path object representing the file where you want to write into:
现在你可以用一行代码来做到这一点。创建表示要写入的文件的 arrayList 和 Path 对象:
Path out = Paths.get("output.txt");
List<String> arrayList = new ArrayList<> ( Arrays.asList ( "a" , "b" , "c" ) );
Create the actual file, and fill it with the text in the ArrayList:
创建实际文件,并用 ArrayList 中的文本填充它:
Files.write(out,arrayList,Charset.defaultCharset());
回答by Joseph Selvaraj
If you need to create each ArrayList item in a single line then you can use this code
如果您需要在一行中创建每个 ArrayList 项,则可以使用此代码
private void createFile(String file, ArrayList<String> arrData)
throws IOException {
FileWriter writer = new FileWriter(file + ".txt");
int size = arrData.size();
for (int i=0;i<size;i++) {
String str = arrData.get(i).toString();
writer.write(str);
if(i < size-1)**//This prevent creating a blank like at the end of the file**
writer.write("\n");
}
writer.close();
}
回答by chao_chang
I would suggest using FileUtils from Apache Commons IO library.It will create the parent folders of the output file,if they don't exist.while Files.write(out,arrayList,Charset.defaultCharset());
will not do this,throwing exception if the parent directories don't exist.
我建议使用 Apache Commons IO 库中的 FileUtils。如果它们不存在,它将创建输出文件的父文件夹。 Files.write(out,arrayList,Charset.defaultCharset());
而不会这样做,如果父目录不存在则抛出异常。
FileUtils.writeLines(new File("output.txt"), encoding, list);
回答by Sahar
I think you can also use BufferedWriter :
我认为你也可以使用 BufferedWriter :
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("note.txt")));
String stuffToWrite = info;
writer.write(stuffToWrite);
writer.close();
and before that remember too add
在那之前记得太添加
import java.io.BufferedWriter;