Java IO Tutorial - Java Files.write(Path path, byte[] bytes, OpenOption ... options)








Syntax

Files.write(Path path, byte[] bytes, OpenOption ... options) has the following syntax.

public static Path write(Path path,  byte[] bytes,  OpenOption ... options)   throws IOException

Example

In the following code shows how to use Files.write(Path path, byte[] bytes, OpenOption ... options) method.

/*from  ww  w  .j a  va 2s  .c  om*/
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 ball_path = Paths.get("C:/tutorial/photos", "ball.png");
        byte[] ball_bytes = new byte[]{(byte) 0x89, (byte) 0x50, (byte) 0x4e, };

        try {
            Files.write(ball_path, ball_bytes);
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}