CopyUtil

10 篇文章 0 订阅

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;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值