当前位置: 移动技术网 > IT编程>开发语言>Java > java 实现文件夹的拷贝实例代码

java 实现文件夹的拷贝实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

java 实现文件夹的拷贝实例代码

        这里就直接上代码,废话不多说,很简单很实用。

实例代码:

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;


public class copyfile {

  public static void copy(string sourcefile , string targetfile) throws exception{
    fileinputstream in = null;
    fileoutputstream out = null;
    try{
      in = new fileinputstream(new file(sourcefile));
      out = new fileoutputstream(new file(targetfile));
      int c;
      while ((c = in.read()) != -1 ){
        out.write(c);
      }
    }
    finally{
      if (in != null){
        in.close();
      }
      if(out != null){
        out.close();
      }
    }
  }

  public static void main(string[] agrs) throws exception{
    string filedir = "./tupu0";
    string targetdir = "./movielist/";
    file directory = new file(filedir);
    file[] filelist = directory.listfiles();
    for(int i=0; i<filelist.length; i++){
      string sourcefile = "./tupu0/" + filelist[i].getname() + "/" + filelist[i].getname() +".txt";
      string targetfile = targetdir + filelist[i].getname();
      system.out.println(filelist[i].getname());
      copy(sourcefile, targetfile);
    }
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网