
package cn.itcast_05; |
import java.io.BufferedInputStream; |
import java.io.BufferedOutputStream; |
import java.io.File; |
import java.io.FileInputStream; |
import java.io.FileOutputStream; |
import java.io.IOException; |
/* |
* 复制文件夹: |
* |
* 数据源:e:\\demo |
* 目的:e:\\test |
* |
*/ |
public class CopyFolderDemo { |
public static void main(String[] args) throws IOException { |
//源文件夹 |
File srcFolder = new File("f:\\demo"); |
//目的文件夹 |
File destFolder = new File("f:\\test"); |
if(!destFolder.exists()){ |
destFolder.mkdirs(); |
} |
|
File[] fileArray = srcFolder.listFiles(); |
|
for(File file:fileArray){ |
//拼接新的文件名和路径 |
File newFile = new File(destFolder,file.getName()); |
System.out.println(newFile); |
|
//调用方法进行复制 |
CopyFile(file,newFile); |
|
|
|
} |
|
} |
private static void CopyFile(File file, File newFile) throws IOException { |
//创建源对象 |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); |
//创建目的对象 |
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); |
|
//读写数据 |
byte [] bt = new byte[1024]; |
int len= 0 ; |
while((len=bis.read(bt))!=-1){ |
bos.write(bt, 0, len); |
} |
|
//释放资源 |
bis.close(); |
bos.close(); |
|
|
} |
} |




by: 发表于:2017-06-27 15:23:23 顶(0) | 踩(0) 回复
??
回复评论