Hus*_*r28 0 java arraylist filewriter
我有一个存储在ArrayList中的一些命令的列表,我一直试图将这个列表打印在一个文本文件中.以下是代码 -
try {
FileWriter writer = new FileWriter("Commands.txt");
for(String str: commandArray){
writer.write(commandArray[i]);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我没有错误,但也没有创建文件.有没有我做错了.任何帮助表示赞赏.非常感谢.
编辑问题
@Sajal Dutta,我接受了你的建议并想出了以下内容;
try {
for(String str: movementArray){
File file = new File("D://filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str);
bw.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在创建文件的那一刻,但只有arraylist的最后一个值被打印到文件中.arraylist中存储了六个值,如何将所有六个值打印出来?
| 归档时间: |
|
| 查看次数: |
6452 次 |
| 最近记录: |