Writing to a file using the BufferedWriter class - Java File Path IO

Java examples for File Path IO:Text File

Description

Writing to a file using the BufferedWriter class

Demo Code

import java.io.BufferedWriter;
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;

public class Main {
  public static void main(String[] args) throws IOException {
    try {//w  ww  .  ja  v a 2  s  . c  o  m
      String newName = "Vivian";
      Path file = Paths.get("/home/docs/users.txt");
      try (BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(), StandardOpenOption.APPEND)) {
        writer.newLine();
        writer.write(newName, 0, newName.length());
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}

Related Tutorials