File Copy as Bytes
public class FileBytesCopy {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fin1 = new FileInputStream("D://file1.txt");
FileOutputStream fout1 = new FileOutputStream("D://file2.txt");
int ch1;
byte[] b1 = new byte[1024];
while (((ch1 = (fin1.read(b1))) != -1)) {
fout1.write(b1, 0, ch1);
}
System.out.println("file1.txt copied to file2.txt");
// same code can be used for copying images fronm one location to another
FileInputStream fin2 = new FileInputStream("D://Desert.jpg");
FileOutputStream fout2 = new FileOutputStream("E://myImage.jpg");
int ch2;
byte[] b2 = new byte[1024];
while (((ch2 = (fin2.read(b2))) != -1)) {
fout2.write(b2, 0, ch2);
}
System.out.println("Desert.jpg copied to nyImage.jpg");
}
}
Output:
file1.txt copied to file2.txt
Desert.jpg copied to myImage.jpg