I'm trying to make a simple program to copy file of any type. I write the code as below.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
public class CopyExample {
public static void main(String[] args) throws Exception {
File f = new File("image.jpg");
FileInputStream is = new FileInputStream(f);
FileOutputStream os = new FileOutputStream("copy-image.png");
byte[] ar = new byte[(int)f.length()];
is.read(ar);
os.write(ar);
is.close();
os.close();
}
}
I already tested this code for .txt
, .jpg
, .png
, .pdf
It is working fine.
But I want to ask is it fine? or is there any other way to do this in better way?