Java Files write list of String to file

Description

Java Files write list of String to file

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("one");
    list.add("4");
    list.add("demo2s.com");
    list.add("6");

    Path dest = Paths.get("Main.java");
    Charset cs = Charset.forName("US-ASCII");
    try {/*from w w  w .j  a  v a 2s  . co  m*/
      Path p = Files.write(dest, list, cs, StandardOpenOption.WRITE,
          StandardOpenOption.CREATE);
      System.out.println("saved " + p.toAbsolutePath());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related