我想读取一个arrayList并将这些字符串存储到一个文件中。 这是我的一些代码:
output = new Formatter(nameFile + ".txt");
while (textLines.hasNext()){
output.format(nameFile, textLines);
}
答案 0 :(得分:1)
public class test1 {
public static void main(String args[]) throws IOException{
StringBuffer sb = new StringBuffer();
String filepath = "test.txt";
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
Iterator<String> it = list.iterator();
while(it.hasNext())
{
sb.append(it.next());
sb.append(",");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(filepath));
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
答案 1 :(得分:0)
试试这个
PrintWriter writer = new PrintWriter("file-name.txt");
for(String s:yourList) //iterate your ArrayList here
{
writer.println(s);
}
writer.close();