public class Demo5 {
public static void main(String[] args) throws IOException {
File dir = getFile();
File destDir = new File("D:/sss");
destDir.mkdirs();
kaobei(dir, destDir);
}
public static void kaobei(File dir, File destDir) throws IOException {
if (dir == null || !dir.exists())
return;
File[] file = dir.listFiles();
if (file == null)
return;
for (File f : file) {
if (f.isDirectory()) {
File f2 = new File(destDir, f.getName());
f2.mkdirs();
kaobei(f, f2);
} else {
if (f.getName().endsWith(".java")) {
File name = new File(f, f.getName()
.replace(".java", ".txt"));
f.renameTo(name);
xie(name, destDir);
} else {
xie(f, destDir);
}
}
}
}
public static void xie(File z, File destDir) throws IOException {
File file = new File(destDir, z.getName());
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(z));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
public static File getFile() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要拷贝的文件夹路径");
while (true) {
String line = sc.nextLine();
File dir = new File(line);
if (!dir.exists()) {
System.out.println("没有此路径,请重新输入");
} else if (dir.isFile()) {
System.out.println("输入的是文件路径,请重新输入");
} else {
return dir;
}
}
}
}
上面是我的代码,但是出错了。。求大牛帮忙。 还有,怎么在不改变原文件的名字的情况下拷贝 |