public class CopyUtil {
public static void main(String[] args) throws IOException {
File conf = new File("d:\\user\\sfit0604\\桌面\\config.txt");
int count = copy(
"D:\\zzx\\SVN\\hrss-sap",
"d:\\user\\sfit0604\\桌面\\dest",
readConfigLine(conf)
);
System.out.println("====>copied files number: " + count);
}
public static List<String> readConfigLine(File conf) throws IOException{
List<String> confList = new ArrayList<String>();
ResourceIterator<String> iterator = new FileTextLineIterator(conf);
while(iterator.hasNext()){
String txt = iterator.next();
txt = txt.trim();
if(txt.length() > 0){
confList.add(txt);
}
}
iterator.close();
return confList;
}
public static int copy(String srcBaseDir, String destBaseDir, List<String> relativePaths) {
int successCount = 0;
for(String path : relativePaths){
File srcFile = new File(srcBaseDir + File.separator + path);
File destFile = new File(destBaseDir + File.separator + path);
if(srcFile.isDirectory()){
destFile.mkdirs();
successCount++;
}else if(srcFile.isFile()){
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdirs();
}
try {
destFile.createNewFile();
} catch (IOException e) {
System.out.println("Create new file failure!");
continue;
}
try {
copyFile(srcFile, destFile);
successCount++;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
return successCount;
}
private static void copyFile(File src, File dest) throws Exception{
if(!src.exists()){
throw new Exception("Source file not exists!");
}
if(!src.isFile()){
throw new Exception("Source is not file!");
}
if(dest.isDirectory()){
throw new Exception("Destination file is directory!");
}
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
byte [] buf = new byte[2048];
int len = 0;
while((len = is.read(buf)) > 0){
os.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
System.out.println("File not exists!");
} catch (IOException e) {
throw e;
} finally {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
}
}
interface ResourceIterator<T> extends Iterator<T>, Closeable {
}
static class FileTextLineIterator implements ResourceIterator<String> {
public static final int DEFAUT_BUF_SIZE = 10 * 1024;
public static final String DEFAUT_ENCODE = Charset.defaultCharset().name();
private int bufSize;
private String encode;
private File file;
private BufferedReader reader;
private String tmp;
public FileTextLineIterator(File file){
this(file, DEFAUT_BUF_SIZE);
}
public FileTextLineIterator(File file, int bufSize){
this(file, DEFAUT_ENCODE, bufSize);
}
public FileTextLineIterator(File file, String encode){
this(file, encode, DEFAUT_BUF_SIZE);
}
public FileTextLineIterator(File file, String encode, int bufSize){
this.file = file;
this.bufSize = bufSize;
this.encode = encode;
}
public void init() throws IOException {
InputStream in = new FileInputStream(this.file);
this.reader = new BufferedReader(new InputStreamReader(in, encode), this.bufSize);
}
@Override
public boolean hasNext() {
try {
if(this.reader == null){
init();
}
if(this.tmp == null){
this.tmp = this.reader.readLine();
}
return (this.tmp != null);
} catch (IOException e) {
throw new RuntimeException();
}
}
@Override
public String next() {
String ret = null;
if(this.tmp != null){
ret = new String(this.tmp);
}
this.tmp = null;
return ret;
}
@Override
public void remove() {
}
@Override
public void close() throws IOException {
if(this.reader != null){
this.reader.close();
}
}
public int getBufSize() {
return bufSize;
}
public void setBufSize(int bufSize) {
this.bufSize = bufSize;
}
public String getEncode() {
return encode;
}
public void setEncode(String encode) {
this.encode = encode;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
}