Writing Lines with the write() Method - Java File Path IO

Java examples for File Path IO:File Operation

Description

Writing Lines with the write() Method

Demo Code

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;

public class Main {
  public static void main(String[] args) {
    Path rf_wiki_path = Paths.get("C:/folder1/wiki", "wiki.txt");
    Charset charset = Charset.forName("UTF-8");
    ArrayList<String> lines = new ArrayList<>();
    lines.add("\n");
    lines.add("a");
    lines.add("test");
    lines.add("test");
    lines.add("test");
    lines.add("test");
    lines.add("test");

    try {/*  w  w w.j a va  2 s  .co m*/
      Files.write(rf_wiki_path, lines, charset, StandardOpenOption.APPEND);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials