- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class fuzhiFile {
- public static void main(String[] args) {
- File file = new File("D:\\myfile");
- String filepath="E:\\";
- search(file,filepath);
-
- }
- public static void search(File file,String filepath){
- if(file.exists()){
- if(file.isDirectory()){
- File file2 = new File(filepath,file.getName());
- file2.mkdirs();
- File[] files = file.listFiles();
- for(File f:files){
- search(f,file2.getAbsolutePath());
- }
- }else{
- fuzhiFiles(file,filepath);
- }
- }
- }
- private static void fuzhiFiles(File file, String filepath) {
- BufferedInputStream bi= null;
- BufferedOutputStream bo= null;
- byte[] buf= new byte[1024*1024];
- int len = -1;
- try {
- bi = new BufferedInputStream(new FileInputStream(file));
- bo = new BufferedOutputStream(new FileOutputStream(new File(filepath,file.getName())));
- while((len =bi.read(buf))!=-1){
- bo.write(buf,0,len);
- bo.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(bi!=null)
- try {
- bi.close();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(bo!=null)
- try {
- bo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码
抛砖引玉 |