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

Java examples for File Path IO:File Operation

Description

Writing Bytes with the write() Method

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path ball_path = Paths.get("C:/folder1/photos", "ball.png");

    byte[] ball_bytes = new byte[] { (byte) 0x89, (byte) 0x50, (byte) 0x4e,
        (byte) 0x47, (byte) 0x0d, (byte) 0x0a, (byte) 0x1a, (byte) 0x0a,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0d, (byte) 0x49,
        (byte) 0x60, (byte) 0x82 };

    try {//from  w  ww.j  a v  a2s. c  om
      Files.write(ball_path, ball_bytes);
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

Result


Related Tutorials