看起来不错,但内容没有用 write() 方法写入文件。我使用 JoptionPane 询问用户输入并将该数据添加到 ArrayList 。添加了数据,但是当我尝试将该数据输出到文件中时,它没有写入文件。public class fileArray {public static void main(String[] args) throws IOException { ArrayList al = new ArrayList(); File f =new File("notworking.txt"); String names = " "; while(!names.isEmpty()) { names=JOptionPane.showInputDialog("EnterName"); if(!names.isEmpty()){ al.add(names);} } FileWriter fw = new FileWriter(f.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); int sz= al.size(); for(int i =0;i<sz;i++){ bw.write((String) al.get(i)); System.out.println(al.get(i)); }}}
3 回答
互换的青春
TA贡献1797条经验 获得超6个赞
示例代码:
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close(); // CLOSE
添加回答
举报
0/150
提交
取消