文件夾用來把文件包裹起來,褪去這些外衣,說到底拷貝文件夾也就是拷貝文件
模擬實例:將F:/Picture/test 文件夾 拷貝到?F:/Picture/dir文件夾
該實例中test文件夾下只包含了test.txt文件
步驟分析:
1.通過路徑得到File對象
2.遞歸查找子孫級文件夾或者文件
3.復制文件(同文件拷貝)
那么重點是在第二個步驟,我們可以通過File對象的listFiles方法得到目標文件夾下所包括的文件,listFiles方法返回一個泛型為File的集合list,由此我們就得到了test文件夾下所有的文件,通過foreach循環語句遍歷這個list,得到的每一個File對象,首先要做的就是判斷這個File對象是文件還是文件夾,如果是文件就可直接copy,如果是文件夾,則需要再通過listFiles方法得到目標文件夾下所包括的文件,步驟與上面一致,這也就是遞歸的思想
需要注意的一點是,我們需要把整個test文件夾拷貝到dir文件夾,那么當遍歷到test文件夾下的test.txt文件時,我們在拷貝的時候,需要重新創建一個新的目標文件,dir/test/text.txt.,這就需要File的另一個構造方法
File(File?parent,?String?child)
?
根據 parent 抽象路徑名和 child 路徑名字符串創建一個新?File
?實例
在得到dir這個文件夾的時候,也應該用上述構造方法,得到dir/testFile新對象
在拷貝文件的時候,使用了不同的流,
之前拷貝文件使用的FileInputStream與FileOutputStream,
這里使用了BufferedInputStream與BufferedOutputStream,使用方法相似
InputStream is =new BufferedInputStream(new FileInputStream(src)); OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));
?
?
?
package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 FileUtil {?
/**?
* @param args?
*/?
public static void main(String[] args) {?
// 源目錄?
String srcPath = "C:\\Users\\Administrator\\Desktop\\bbb";?
// 目標目錄?
String destPath = "C:\\Users\\Administrator\\Desktop\\ccc";?
//進行拷貝?
try {?
copyDir(srcPath, destPath);?
} catch (FileNotFoundException e) {?
// TODO Auto-generated catch block?
e.printStackTrace();?
} catch (IOException e) {?
// TODO Auto-generated catch block?
e.printStackTrace();?
}?
}?
/**?
* 通過路徑獲得File對象?
*?
* @param src源路徑?
* @param dest目標路徑?
* @throws IOException?
* @throws FileNotFoundException?
*/?
public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{?
//拒絕自己拷貝給自己?
if(srcPath.equals(destPath)){?
return ;?
}?
File src=new File(srcPath);?
File dest =new File(destPath);?
copyDir(src,dest);?
}?
/**?
* 拷貝文件夾?
* @param src 源File對象?
* @param dest 目標File對象?
* @throws IOException?
* @throws FileNotFoundException?
*/?
public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{?
if(src.isDirectory()){ //文件夾?
dest =new File(dest,src.getName());?
if(dest.getAbsolutePath().contains(src.getAbsolutePath())){?
System.out.println("父目錄不能拷貝到子目錄中");?
return;?
}?
}?
copyDirDetail(src,dest);?
}?
/**?
* 拷貝文件夾細節?
* @param src?
* @param dest?
*/?
public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{?
if(src.isFile()){ //文件?
copyFile(src, dest);?
}else if(src.isDirectory()){ //文件夾?
//確保目標文件夾存在?
dest.mkdirs();?
//獲取下一級目錄|文件?
for(File sub:src.listFiles()){?
copyDirDetail(sub,new File(dest,sub.getName()));?
}?
}?
}?
/**?
* 文件的拷貝,得到File對象?
* @param 源文件路徑?
* @param 目錄文件路徑?
* @throws FileNotFoundException,IOException?
* @return?
*/?
public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {?
//1、建立聯系 源(存在且為文件) +目的地(文件可以不存在)?
copyFile(new File(srcPath),new File(destPath));?
}?
/**?
* 文件的拷貝?
* @param 源文件File對象?
* @param 目錄文件File對象?
* @throws FileNotFoundException,IOException?
* @return?
*/?
public static void copyFile(File src,File dest) throws FileNotFoundException,IOException {?
if(! src.isFile()){ //不是文件或者為null?
System.out.println("只能拷貝文件");?
throw new IOException("只能拷貝文件");?
}?
//dest為已經存在的文件夾,不能建立于文件夾同名的文件?
if(dest.isDirectory()){?
System.out.println(dest.getAbsolutePath()+"不能建立于文件夾同名的文件");?
throw new IOException(dest.getAbsolutePath()+"不能建立于文件夾同名的文件");?
}?
//2、選擇流?
InputStream is =new BufferedInputStream(new FileInputStream(src));?
OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));?
//3、文件拷貝 循環+讀取+寫出?
byte[] flush =new byte[1024];?
int len =0;?
//讀取?
while(-1!=(len=is.read(flush))){?
//寫出?
os.write(flush, 0, len);?
}?
os.flush(); //強制刷出?
//關閉流?
os.close();?
is.close();?
}?
}