How to write an ArrayList of Strings into a text file?

后端 未结 7 1140
不思量自难忘°
不思量自难忘° 2020-11-27 15:47

I want to write an ArrayList into a text file.

The ArrayList is created with the code:

ArrayList arr = new A         


        
7条回答
  •  感动是毒
    2020-11-27 16:19

    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:

    Path out = Paths.get("output.txt");
    List arrayList = new ArrayList<> ( Arrays.asList ( "a" , "b" , "c" ) );
    

    Create the actual file, and fill it with the text in the ArrayList:

    Files.write(out,arrayList,Charset.defaultCharset());
    

提交回复
热议问题