|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- package cs08;
- import java.io.*;
- public class copyfolderdemo {
- public static void main(String[] args) throws IOException {
- File srcFolder=new File("E:\\filecs");
- String srcFolderName= srcFolder.getName();
- File destFolder =new File("E:\\cs\\filecs",srcFolderName);
- if (!destFolder.exists()){
- destFolder.mkdir();
- }
- File[] listFiles=srcFolder.listFiles();
- for (File srcFile : listFiles){
- String srcFileName=srcFile.getName();
- File destFile =new File(destFolder,srcFileName);
-
- copyFile(srcFile,destFile);
- }
- }
- private static void copyFile(File srcFile,File destFile) throws IOException {
- BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
- BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
- byte[] bys=new byte[1024];
- int len;
- while((len=bis.read(bys))!=-1){
- bos.write(bys,0,len);
- }
- bos.close();
- bis.close();
- }
- }
复制代码
我想问一下为什么 for (File srcFile : listFiles){
String srcFileName=srcFile.getName();
File destFile =new File(destFolder,srcFileName);
为什么增强for循环里要获取源文件和目的地文件的file对象?删了还运行不起来 |
|