how to copy content to new file in java code example
Example 1: java copy file from one directory to another efficiently
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
Example 2: copy file with byte java
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
FileInputStream in =null;
FileOutputStream out=null;
try {
in=new FileInputStream(args[0]);
out=new FileOutputStream(args[1]);
int c;
while ((c=in.read())!=-1){
out.write(c);
}
}
catch (Exception e){
System.out.println("Error Copping File");
}
}
}