package com.awy.util.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* This is the utility class to copy file from one place to another
*
* @author awayyout
*
*/
public class FileInstaller {
private String sourceFolderPath;
private String targetFolderPath;
/**
* To construct file installer
*
* @param sourcePath,
* the path of source files
* @param targetPath,
* the target path to copy files to
*/
public FileInstaller(String sourcePath, String targetPath) {
this.sourceFolderPath = sourcePath;
this.targetFolderPath = targetPath;
}
/**
* Copy all the file from source to target
*
* @throws IOException,
* if file not found or any problem during copy files
*/
public void copyAll() throws IOException {
File targetDir = new File(this.targetFolderPath);
File sourceDir = new File(this.sourceFolderPath);
copyDirectory(sourceDir, targetDir);
}
private void copyDirectory(File srcPath, File dstPath) throws IOException {
if (srcPath.isDirectory()) {
if (!dstPath.exists()) {
dstPath.mkdir();
}
String files[] = srcPath.list();
for (int i = 0; i <>
copyDirectory(new File(srcPath, files[i]), new File(dstPath,
files[i]));
}
}
else {
InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(dstPath);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
No comments:
Post a Comment