Save to a file with buffered stream - Java File Path IO

Java examples for File Path IO:File Operation

Description

Save to a file with buffered stream

Demo Code

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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) {
    Path path = Paths.get("C:/folder1/equipment", "test.txt");
    String string = "\ntest";

    try (OutputStream outputStream = Files.newOutputStream(path,
        StandardOpenOption.APPEND);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
            outputStream))) {/*  ww  w .  ja v  a2  s . c  om*/
      writer.write(string);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials