This example is demonstrate how to copy file in java from source to destination directory.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileCopier {
public static void main(String args[]) throws Exception {
File inboxDirectory = new File("D:/uttam/app/eclipse/Test class/src/data/inbox");
File outboxDirectory = new File("D:/uttam/app/eclipse/Test class/src/data/outbox");
outboxDirectory.mkdir();
File[] files = inboxDirectory.listFiles();
System.out.println(files);
try{for (File source : files) {
if (source.isFile()) {
File dest = new File(outboxDirectory.getPath() + File.separator
+ source.getName());
copyFile(source, dest);
}
}}
catch(Exception e){}
}
private static void copyFile(File source, File dest) throws IOException {
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[(int) source.length()];
FileInputStream in = new FileInputStream(source);
in.read(buffer);
try {
out.write(buffer);
} finally {
out.close();
in.close();
}
}
}
No comments:
Post a Comment