java如何拷贝一个文件夹内的多个指定的文件到另外一个指定的文件夹下?

如题所述

你好:

请看代码:

/**
* 把一个文件夹里的所有文件包括文件夹 一并原样拷贝到另一个目录中;
*@author shuishui
*/  
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
  
public class CopyDir001 {   
  
    public static File dirFrom;   
    public static File dirTo;   
  
    // 目标路径创建文件夹   
    public void listFileInDir(File file) {   
         File[] files = file.listFiles();   
        for (File f : files) {   
             String tempfrom = f.getAbsolutePath();   
             String tempto = tempfrom.replace(dirFrom.getAbsolutePath(),   
                     dirTo.getAbsolutePath()); // 后面的路径 替换前面的路径名   
            if (f.isDirectory()) {   
                 File tempFile = new File(tempto);   
                 tempFile.mkdirs();   
                 listFileInDir(f);   
             } else {   
                 System.out.println("源文件:" + f.getAbsolutePath());   
                //   
                int endindex = tempto.lastIndexOf("\\");// 找到"/"所在的位置   
                 String mkdirPath = tempto.substring(0, endindex);   
                 File tempFile = new File(mkdirPath);   
                 tempFile.mkdirs();// 创建立文件夹   
                 System.out.println("目标点:" + tempto);   
                 copy(tempfrom, tempto);   
             }   
         }   
     }   
    /**
      * 封装好的文件拷贝方法
      */  
    public void copy(String from, String to) {   
        try {   
             InputStream in = new FileInputStream(from);   
             OutputStream out = new FileOutputStream(to);   
  
            byte[] buff = new byte[1024];   
            int len = 0;   
            while ((len = in.read(buff)) != -1) {   
                 out.write(buff, 0, len);   
             }   
             in.close();   
             out.close();   
         } catch (FileNotFoundException e) {   
             e.printStackTrace();   
         } catch (IOException e) {   
             e.printStackTrace();   
         }   
     }   
  
    public static void main(String[] args) {   
         File fromfile = new File("e:\\shui\\test");// 源文件夹   
         File tofile = new File("e:\\Jying\\shui");// 目标   
  
         CopyDir001 copy = new CopyDir001();   
        // 设置来源去向   
         copy.dirFrom = fromfile;   
         copy.dirTo = tofile;   
         copy.listFileInDir(fromfile);   
  
     }   
}

追问

我只想拷贝文件夹下几个固定的文件,比如一个文件下有1.txt 1111.txt dd.txt 我只想拷贝1.txt和dd.txt两个文件。不需要整个文件夹所以文件进行拷贝

追答

自己试着改一下哦!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-09
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public void copyFolder(String oldPath, String newPath) {

try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}

if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();

}

}追问

我只想拷贝文件夹下几个固定的文件,比如一个文件下有1.txt 1111.txt dd.txt 我只想拷贝1.txt和dd.txt两个文件。不需要整个文件夹所以文件进行拷贝

相似回答