Java IO Tutorial - Java Files.newOutputStream(Path path, OpenOption ... options)








Syntax

Files.newOutputStream(Path path, OpenOption ... options) has the following syntax.

public static OutputStream newOutputStream(Path path,    OpenOption ... options)     throws IOException

Example

In the following code shows how to use Files.newOutputStream(Path path, OpenOption ... options) method.

//w  w w  .j a  v  a 2 s .  c om
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 rn_demo = Paths.get("C:/tutorial/Java", "demo.txt");
        String demo = "tutorial";
        String string = "\nString: from java2s.com";

        //using NIO.2 unbuffered stream
        byte data[] = demo.getBytes();
        try (OutputStream outputStream = Files.newOutputStream(rn_demo)) {
            outputStream.write(data);
        } catch (IOException e) {
            System.err.println(e);
        }


    }
}