import java.io.*; public class CopyBytes { public static void main(String[] args) throws IOException { String filename = "lena.png"; try (FileInputStream in = new FileInputStream(filename); FileOutputStream out = new FileOutputStream("__"+filename)) { byte []array = new byte[512]; int length; while ((length = in.read(array)) != -1) { System.out.println(length); out.write(array, 0, length); } } } }