import java.io.*; public class CopyBufferedBytes { public static void main(String[] args) throws IOException { String filename = "lena.png"; try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename)); BufferedOutputStream out = new BufferedOutputStream(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); } } } }