package practiceWrong;
import java.io.*;
//利用FileOutputStream 和 FileInputStream copy
//传进来的应该是两个字符串,代表着从哪里复制,以及复制到哪里
class CopyTest01 {
public void copy(String in , String out){
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(in);
output = new FileOutputStream(out);
byte[] bytes = new byte[1024 * 1024];//1MB
int readCount = 0;
while ((readCount = input.read(bytes)) != -1){
output.write(bytes,0,readCount);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(output != null){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void copyFileJia(String luJing02){
File file1 = new File(luJing02);
file1.mkdirs();
//如果:\file不存在,以目录的形式创建出来
/*if(!f1.exists()){
f1.mkdir();
}*/
}
}
package practiceWrong;
import java.io.File;
class GetFile {
CopyTest01 file01 = new CopyTest01();
public String getFile(String in) { //假设起始文件夹是1
File[] file02 = new File(in).listFiles();
for (File fi : file02) {
//File file1 = new File("fi");
if (fi != null) {
if (!(new File("fi").isDirectory())) {
//如果不是目录的话,就返回这个文件的绝对路径
//问题:有这个绝对路径,但是I盘怎么写
fi.getAbsolutePath();
String fi02 ="I:".concat(fi.toString());
file01.copy( fi.toString(),fi02);
} else {
//如果是目录的话,就该递归调用它自己,假设这个文件夹名2,里面有3和4文件夹,
//3里面又有5文件,4里面有6文件
//拷贝文件夹,其实是返回文件夹的字符串
String wenJianJia = fi.toString();
file01.copyFileJia("I:" + wenJianJia);
return getFile(fi.toString());
}
}
}
return "success";
}
}
package practiceWrong;
/**
* copy一个文件夹过来
* 想法:1、写一个类封装一下copy文件的方法(FileInputStream 和 FileOutputStream方法一般读一边写)
* 2、利用listFiles()方法获取一个文件夹里所有的文件,若获取的文件夹里String不为null,则继续
* 获取,直到为null,说明没有文件夹了,只有文件了(或者说文件夹里没有文件),再判断是否要调用copy方法
* 3、要注意,第二步是所有文件夹同时进行的,不能只进行一个,否则的话,难以回到开始文件夹的下面打开其他的文件夹
*/
public class Test01 {
public static void main(String[] args) {
GetFile ss = new GetFile();
ss.getFile("H:\\editPlus");
}
}