将不同目录下的文件,复制到同一个目录下。

来源:互联网 发布:4g手机只能用2g网络 编辑:程序博客网 时间:2024/05/17 01:56
import java.io.*;
public class FileCopy
{
    public  void copyfile(String origpath,String name, String nowpath) throws IOException //使用FileInputStream和FileOutStream
    {
       
        File nowcontent = new File(nowpath);
       
        if(!nowcontent.exists())
        {
            nowcontent.mkdirs();
        }
        nowcontent = null;
        File origFile = new File(origpath,name);
       
        File nowFile = new File(nowpath,name);
       
        FileInputStream fi = new FileInputStream(origFile);
        FileOutputStream fo = new FileOutputStream(nowFile);
        byte data[] = new byte[2048];
        int len = 0;
  while((len=fi.read(data))>0)
        fo.write(data,0,len);
        fi.close();
        fo.close();
        nowFile = null;
        origFile = null;
   }
   
    public static void main(String[] args)
    {
        try
        {
            FileCopy fc = new FileCopy();
            fc.copyfile("C://","Hello.java","F://abc");
            fc.copyfile("d://","ad.txt","F://abc");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
原创粉丝点击