package com.wonders.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Date; /** * Copy all files under the folder (including files under subfolders) to the target (same level) (and edit the files) * @author Admin * */ public class DirErgodic { private static int depth = 1; private static int size = 1; /** * Copy all files under the folder (including files under subfolders) to the target (same level) * @param pathName * @param depth * @param target * @throws IOException */ public static void find(String pathName,int depth,String target) throws IOException{ int filecount=0; //Get the File object of pathName File dirFile = new File(pathName); //Determine whether the file or directory exists, and output a reminder on the console if it does not exist if (!dirFile.exists()) { System.out.println("do not exit"); return ; } File targetDir = new File(target); //Determine whether the file or directory exists, and output a reminder on the console if it does not exist if (!targetDir.exists()) { targetDir.mkdirs(); } / / Determine if it is not a directory, then determine whether it is a file, if the file is the output file path if (!dirFile.isDirectory()) { if (dirFile.isFile()) { System.out.println(dirFile.getCanonicalFile()); } return ; } for (int j = 0; j < depth; j++) { System.out.print(" "); } System.out.print("|--"); System.out.println(dirFile.getName()); //Get all file names and directory names in this directory String[] fileList = dirFile.list(); int currentDepth=depth+1; for (int i = 0; i < fileList.length; i++) { // Traverse the file directory String string = fileList[i]; //File("documentName","fileName") is another constructor of File File file = new File(dirFile.getPath(),string); String name = file.getName(); //If it is a directory, search depth++, after outputting the directory name, perform recursion if (file.isDirectory()) { //recursive find(file.getCanonicalPath(),currentDepth,target); }else{ // copyFile(file,new File(target+"\\"+name)); updateToNewHtmlFile(file,new File(target+"\\"+name)); //If it is a file, output the file name directly for (int j = 0; j < currentDepth; j++) { System.out.print(" "); } System.out.print("|--"); System.out.println(size+": "+name); size++; } } } // copy the file public static void copyFile(File sourceFile, File targetFile) throws IOException { BufferedInputStream inBuff = null; BufferedOutputStream outBuff = null; try { inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } outBuff.flush(); } finally { if (inBuff != null) inBuff.close(); if (outBuff != null) outBuff.close(); } } public static boolean updateToNewHtmlFile(File sourceFile, File targetFile) { String str = ""; long beginDate = (new Date()).getTime(); str = getValue(readfile(sourceFile)); try { BufferedWriter o = new BufferedWriter(new FileWriter(targetFile)); o.write(str); o.close(); System.out.println("Shared time: " + ((new Date()).getTime() - beginDate) + "ms"); } catch (IOException e) { e.printStackTrace (); return false; } return true; } public static String readfile(File file) { // save the original format InputStream input = null; try { input = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace (); } StringBuffer buffer = new StringBuffer(); byte[] bytes = new byte[1024]; try { for (int n; (n = input.read(bytes)) != -1;) { buffer.append(new String(bytes, 0, n, "GBK")); } } catch (IOException e) { e.printStackTrace (); } //output file as one line: kick out blank lines // StringBuffer buffer = new StringBuffer(); // try { // String tempStr = ""; // FileInputStream is = new FileInputStream(file);// Read module file // BufferedReader br = new BufferedReader(new InputStreamReader(is)); // while ((tempStr = br.readLine()) != null) // buffer.append(tempStr); // is.close(); // } catch (IOException e) { // e.printStackTrace (); // } return buffer.toString(); } public static String getValue(String val) { String end = "</html>"; int e = val.indexOf(end)+ end.length(); return val.substring(0, e); } public static void main(String[] args) throws IOException{ // Starting time Long begin = new Date().getTime(); find("D:\\source\\annex", depth,"E:\\a"); // End Time Long end = new Date().getTime(); // time consuming System.out.println("Time spent: " + (end - begin) / 1000 + " s"+""); } }
java move file and edit file content
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326081196&siteId=291194637
Recommended
Ranking