IO 文件夹的拷贝

it2022-05-05  104

package FileCopy; 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.InputStream; import java.io.OutputStream; import java.util.LinkedList; import java.util.Queue; public class CopyFileV2 { public static void main(String agrs[]){ String srcFileName = "F:" + File.separator + "ppt"; String destFilePath = "D:" + File.separator + "copyFileExam"; // copy(srcFileName, destFilePath); rNoCopy(srcFileName, destFilePath); System.out.println("拷贝完成!!!"); } private static void rNoCopy(String fileName, String destFileName){ Queue<String[]> fPQ = new LinkedList<String[]>(); String[] s = new String[2]; s[0] = fileName; s[1] = destFileName; fPQ.offer(s); int i = 0; while(!fPQ.isEmpty()){ for(String[] st : fPQ){ for(String str : st) System.out.println(str); } System.out.println("\n" + i++); String[] filePath = fPQ.poll(); File file = new File(filePath[0]); File destFile = new File(filePath[1]); if(!destFile.exists()){ destFile.mkdirs(); System.out.println("创建多级列表成功!!!"); } File files[] = file.listFiles(); for(File f: files){ if(f.isFile()){ fileCopy(f.getPath(), filePath[1] + File.separator + f.getName()); } else if(f.isDirectory()){ String[] temps = new String[2]; temps[0] = f.getPath(); temps[1] = filePath[1] + File.separator + f.getName(); fPQ.offer(temps); } } } } private static void copy(String fileName, String destFileName){ File file = new File(fileName); File destFile = new File(destFileName); File files[] = file.listFiles(); if(!destFile.exists()){ destFile.mkdirs(); } for(File f: files){ if(f.isFile()){ fileCopy(f.getPath(), destFileName + File.separator + f.getName()); } else if(f.isDirectory()){ copy(f.getPath(), destFileName + File.separator + f.getName()); } } } private static void fileCopy(String fileName, String destFileName){ System.out.println("正在拷贝文件!!!"); File file = new File(fileName); File destFile = new File(destFileName); // InputStream is = null; // OutputStream os = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(file)); bos = new BufferedOutputStream(new FileOutputStream(destFile)); byte bytes[] = new byte[1024]; while( bis.read(bytes) != -1 ){ bos.write(bytes); bos.flush(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // os.flush(); // is.close(); // os.close(); bos.flush(); bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } // private static void getAllFileList(String fileName){ // File file = new File(fileName); // File files[] = file.listFiles(); // // for(File f: files){ // if(f.isFile()){ // // } // if(f.isDirectory()){ // getAllFileList(f.toString()); // } // else { // System.out.println(f.getName()); // } // } // // } } }

 

转载于:https://www.cnblogs.com/854594834-YT/p/10527471.html


最新回复(0)