如果将D:\360Downloads\day18文件夹里的文件全复制到D:\360Downloads\day18\day18_video中去.我写出的代码如:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) throws IOException {
// (3)从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
File file1 = judge();
File file2 = judge();
copy(file1, file2);
}
public static File judge() {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入文件夹路径");
String s = sc.nextLine();
File file = new File(s);
if (!file.exists()) {
System.out.println("您输入路径不存在");
} else {
if (file.isDirectory()) {
return file;
} else {
System.out.println("您输入的是文件路径");
}
}
}
}
public static void copy(File file1, File file2) throws IOException {
File[] files = file1.listFiles();
if (files != null) {
for (File files11 : files) {
String s = files11.getName();
String s1 = file2.toString();
String s2 = s1 + "\\" + s;
/*char[] ch = s2.toCharArray();
ArrayList<Character> al = new ArrayList<>();
for (char c : ch) {
al.add(c);
}
for (int i = 0; i < al.size(); i++) {
if (al.get(i) == '\\') {
al.add(i, '\\');
i++;
}
}
String s3 = "";
for (int i = 0; i < al.size(); i++) {
s3 = s3 + al.get(i);
}*/
if (files11.isFile()) {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(files11));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(s2));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
} else {
File filea = new File(s2);
filea.mkdirs();
copy(files11, filea);
}
}
}else{
System.out.println("您输入的文件夹是空文件夹,无法复制里面的内容");
}
}
}
发现在day18_video文件夹中会复制文件,之后在复制了的day18_video中又复制文件,之后windows测试的时候,出现提示让我跳过day18_video这个文件夹才能复制!
有什么方法可以让他停止复制,或者实现跳过这个文件夹呢? |
|