java io 文件夹和文件的拷贝

来源:互联网 发布:embedpano.js 编辑:程序博客网 时间:2024/06/06 02:28

文件夹的拷贝,mkdir(),多层mkdirs();

地址要写对,我对地址还是有点问题,如放i盘下abc文件夹里,放这个里面去,然后函数最好别放在主函数里,递归调用不行,功能与主函数分开写。



package homework;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;


import IOStream.filecopy;


/*
 * 1.实现 文件的复制和文件夹的复制  如果指定的位置有相同的文件 提供覆盖功能


 2.统计指定的文件中的 数字  字母  标点  分别一共有多少个  使用StreamTokenizer 


 */
public class c4_1 {
public static void main(String args[]) throws Exception {
File file = new File("H:/学习文件/大三上复习/期末/java/JAVA程序设计实验");
String file2 = new String("i:\\");
(new File(file2)).mkdirs();
File file1 = new File("i:/");
File[] files = file.listFiles();
for (File f : files) {
if (!f.isDirectory()) {
String name = f.getName();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f));
// 无法放入一个文件里
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file1 + name));
int hasread = 0;
byte[] a = new byte[1024 * 4];
while ((hasread = bis.read(a)) != -1) {
bos.write(a);
bos.flush();


}


bos.close();
} else {
String name = f.getName();
(new File(file2 + name)).mkdirs();
String file5=file2+name;
File[] file4 = f.listFiles();
for (File f1 : file4) {
if (!f1.isDirectory()) {
String name1 = f1.getName();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f1));
// 无法放入一个文件里
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file5 + name1));
int hasread = 0;
byte[] a = new byte[1024 * 4];
while ((hasread = bis.read(a)) != -1) {
bos.write(a);
bos.flush();
}
bos.close();
}
}
}


}
}

}



http://blog.csdn.net/dream_javaworld/article/details/3682075   写的不错的范文

0 0