JAVA递归调用 复制文件夹及子文件夹及文件夹下的文件
2014-09-28 03:17
190 查看
import java.io.*;
import java.util.*;
/**
*复制文件
*by lecky.lee
*2006-06-08
*/
public class CopyFile
{
String intiPathOut;
//初始输出路径
int cnt=0;
//计数器,用于判定是否首次调用copyFiles方法
public void copyFiles(String pathIn,String pathOut) throws IOException
{
File f= new File(pathIn);
if(!f.exists()){
System.out.println ("源文件路径不存在");
System.exit(5);
}
if(cnt==0){
File out = new File(this.intiPathOut);
out.mkdir();
//首次调用执行,创建初始输出目录
}
if(f.isFile()){
this.copyFile(f,pathOut);
}
else{
this.copyDir(f,pathOut);
}
cnt++;
}
public void copyDir(File f,String pathOut) throws IOException{
System.out.println(f.getPath());
pathOut=this.intiPathOut+f.getPath().substring(2);
//修改输出路径字符串,将子文件夹相对路径追加至初始输出路径
//substring用于去掉路径中的驱动器盘符
File dir = new File(pathOut);
dir.mkdir();
File temp[]=f.listFiles();
for(int i=0;i this.copyFiles(temp[i].toString(),pathOut);
//递归调用此方法
}
}
public void copyFile(File f,String pathOut) throws IOException{
System.out.println(f.getPath());
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(f.getPath())));
byte[] date = new byte[in.available()];
in.read(date);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(pathOut+"/"+f.getName())));
out.write(date);
in.close();
out.close();
}
public static void main(String[] args) throws IOException
{
CopyFile cp = new CopyFile();
cp.intiPathOut="D:/11111111";
cp.copyFiles("D:/lecky","D:/11111111");
System.out.println ("复制完毕");
}
}
import java.util.*;
/**
*复制文件
*by lecky.lee
*2006-06-08
*/
public class CopyFile
{
String intiPathOut;
//初始输出路径
int cnt=0;
//计数器,用于判定是否首次调用copyFiles方法
public void copyFiles(String pathIn,String pathOut) throws IOException
{
File f= new File(pathIn);
if(!f.exists()){
System.out.println ("源文件路径不存在");
System.exit(5);
}
if(cnt==0){
File out = new File(this.intiPathOut);
out.mkdir();
//首次调用执行,创建初始输出目录
}
if(f.isFile()){
this.copyFile(f,pathOut);
}
else{
this.copyDir(f,pathOut);
}
cnt++;
}
public void copyDir(File f,String pathOut) throws IOException{
System.out.println(f.getPath());
pathOut=this.intiPathOut+f.getPath().substring(2);
//修改输出路径字符串,将子文件夹相对路径追加至初始输出路径
//substring用于去掉路径中的驱动器盘符
File dir = new File(pathOut);
dir.mkdir();
File temp[]=f.listFiles();
for(int i=0;i this.copyFiles(temp[i].toString(),pathOut);
//递归调用此方法
}
}
public void copyFile(File f,String pathOut) throws IOException{
System.out.println(f.getPath());
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(f.getPath())));
byte[] date = new byte[in.available()];
in.read(date);
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(pathOut+"/"+f.getName())));
out.write(date);
in.close();
out.close();
}
public static void main(String[] args) throws IOException
{
CopyFile cp = new CopyFile();
cp.intiPathOut="D:/11111111";
cp.copyFiles("D:/lecky","D:/11111111");
System.out.println ("复制完毕");
}
}
相关文章推荐
- java递归复制文件及文件夹
- java递归实现复制一个文件夹下所有文件的功能
- Java中递归实现复制文件夹和文件
- JAVA 遍历文件夹下的所有文件(递归调用)
- 使用java递归实现文件及文件夹的复制
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- 复制一个文件夹下所有文件(包括子文件夹)并修改文件后缀名 java实现加密文档unlock
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- Java中通过递归调用删除文件夹下所有文件
- 删除文件夹(递归调用删除所有子文件夹及文件)
- Java笔记之io——递归创建与删除多层文件夹,字符流实现文件复制
- java 文件、文件夹 递归复制、移动
- java递归方法复制文件和文件夹
- Node.JS 循环递归复制文件夹目录及其子文件夹下的所有文件
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)<转>
- java通过递归调用删除文件夹下所有文件
- JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
- JAVA实现遍历文件夹下的所有文件(递归调用和非递归调用)