论坛首页 Java企业应用论坛

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

浏览 1877 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-07   最后修改:2008-11-07

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

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics