一、(I/O)流递归复制一个文件
第一种:
else if语句过多,看起来冗余,优点:多级文件一次性复制完整
import java.io.*;
//数据源:src/main/java/day15_8_13/haha
//目标;src/main/java/LaJi
public class DiGuiCopy {
public static void main(String[] args) throws Exception {
File file = new File("src/main/java/day15_8_13/haha");
File file1=new File("src/main/java/LaJiZhan");
diguiCopy(file,file1);
}
public static void diguiCopy(File file,File file1)throws Exception{
if(file.isDirectory()&&file!=null){
File file2s = new File(file1.getPath() + "\\" + file.getName());
System.out.println("新文件夹创建:"+file2s.mkdir());
File[] files = file.listFiles();
if(files!=null){
for (File file2 : files) {
if(file2.isFile()){
diguiCopy(file2,file2s);
} else if (file2.isDirectory()) {
diguiCopy(file2,file2s);
}
}
}
} else if (file.isDirectory()&&file==null) {
//在目标文件下创建相同名字的文件
File file2s = new File(file1.getPath() + "\\" + file.getName());
System.out.println("新文件夹创建:"+file2s.mkdir());
} else if (file.isFile()&&file!=null) {
File file2s = new File(file1.getPath() + "\\" + file.getName());
System.out.println("新文件创建:"+file2s.createNewFile());
//创建字节输入流,创建字节输出流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2s));
byte[] bytes = new byte[2048];
int len=0;
while ((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
bos.flush();
} bos.close();
System.out.println("写入成功");
} else if (file.isFile()&&file==null) {
File file2s = new File((file1.getPath() + "\\" + file.getName()));
System.out.println("空文件创建:"+file2s.createNewFile());
}
}
}
第二种:
如果第一次文件内容未写成功,就运行一次,优点:避免多次if/for死递归出错,代码清晰
import java.io.*;
/*
复制文件夹src/main/java/day15_8_13/haha下面所有文件和子文件夹内容到src/main/java/day15_8_13/haha_A
*/
public class DiGuiCopy1 {
public static void main(String[] args) {
//将目标文件夹封装成File对象
File file = new File("src/main/java/day15_8_13/haha");
// String path = file.getPath();
// System.out.println(path);
fun(file);
}
public static void fun(File file) {
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) {
String filePath = file1.getPath();
String newPath = filePath.replace("haha", "haha_A"); //src/main/java/day15_8_13/haha
//目标文件夹
File file2 = new File(newPath);
file2.mkdirs();
fun(file1);
} else {
//file1 原本的文件
String parent = file1.getPath();
String newPath = parent.replace("haha", "haha_A");//src/main/java/day15_8_13/haha_A
File file2 = new File(newPath);
write(file1, file2);
}
}
}
public static void write(File p, File s) {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(p.getPath()));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s.getPath()));
// BufferedReader br = new BufferedReader(new FileReader(p.getPath()));
// BufferedWriter bw = new BufferedWriter(new FileWriter(s.getPath()));
byte[] bytes = new byte[2048];
int length = 0;
while ((length = bis.read(bytes)) != -1) {
bos.write(bytes,0,length);
bos.flush();
}
bos.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、(I/O)流递归删除一个文件
import java.io.*;
import static day15_8_13.DiGuiCopy.diguiCopy;
//缺点:删除后,还会留存空的根目录
public class DiGuiDelete {
public static void main(String[] args) throws Exception {
File file=new File("src/main/java/LaJiZhan/haha");
diguiDelete(file);
}
public static void diguiDelete(File file){
if(file.isDirectory()&&file!=null){
File[] files = file.listFiles();
if(files!=null){
for (File file1 : files) {
if(file1.isFile()){
diguiDelete(file1);
} else if (file1.isDirectory()) {
diguiDelete(file1);
}
}
} else if (files==null) {
diguiDelete(file);
}
} else if (file.isDirectory()&&file==null) {
//在目标文件下删除相同名字的空文件
System.out.println("空文件夹删除:"+file.delete());
} else if (file.isFile()) {
System.out.println("文件删除:"+file.delete());
}
}
}
标签:file1,输入输出,java,file,递归,多级,File,new,main
From: https://blog.csdn.net/ABU009/article/details/141285976