公司的电脑是有安全检查的,每次复制一个文件到U盘中都要扫描半天,特别的慢,为了避开电脑的扫描,
想到了java中的io流,写的一个使用递归的方式复制文件夹的demo。保存到博客中,跟大家分享一下,
同时也可以方便后面自己查看。
1.先看看复制文件
①使用基本字节流
public void copyFile(String srcPath, String desPath) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(srcPath);
outputStream = new FileOutputStream(desPath);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0,len);
outputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println(">>>>>异常<<<<<");
}finally {
if(inputStream!=null){
try {
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if(outputStream!=null){
try {
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
②使用字节缓冲流复制文件
public void copyFile2(String srcPath, String desPath) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(srcPath));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desPath));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes,0,len);
bufferedOutputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println(">>>>>异常<<<<<");
}finally {
if(bufferedInputStream!=null){
try {
bufferedInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
2.下面是完整的代码
import java.io.*;
/**
* @author ooyhao
* //递归复制文件及文件夹
*/
public class Test {
@org.junit.Test
public void test(){
String srcPath = "G:\\面试资料";
String desPath = "G:\\copyFile";
list(srcPath,desPath);
}
public void list(String srcPath, String descPath){
File srcFile = new File(srcPath);
descPath = descPath + File.separator+srcFile.getName();
System.out.println("srcPath:"+srcPath);
System.out.println("descPath:"+descPath);
//首先判断源目录是否为一个文件夹
if(srcFile.isDirectory()){
//表示当前目录是一个文件夹
//获得当前目录下的所有file对象
File filePath = new File(descPath);
filePath.mkdirs();
File[] files = srcFile.listFiles();
for(File file : files){
list(file.getAbsolutePath(),filePath.getAbsolutePath());
}
}else{
//表示当前目录是一个文件
copyFile2(srcPath,descPath);
}
}
// 使用基本字节流进行复制
public void copyFile(String srcPath, String desPath) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(srcPath);
outputStream = new FileOutputStream(desPath);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0,len);
outputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println(">>>>>异常<<<<<");
}finally {
if(inputStream!=null){
try {
inputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if(outputStream!=null){
try {
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
// 使用字节缓冲流复制文件
public void copyFile2(String srcPath, String desPath) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(srcPath));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desPath));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes,0,len);
bufferedOutputStream.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println(">>>>>异常<<<<<");
}finally {
if(bufferedInputStream!=null){
try {
bufferedInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
标签:outputStream,java,递归,bytes,文件夹,bufferedOutputStream,srcPath,new,null From: https://blog.51cto.com/u_12131813/6002840