递归读取某目录下文件,并写入txt
某目录下有大量文件,且目录层级不限,需要提取文件信息,利用递归读取即可。
public static void folderMethod(String path) { File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (null != files) { for (File file2 : files) { if (file2.isDirectory()) { folderMethod(file2.getAbsolutePath()); } else { System.out.print("文件完整路径:" + file2.getAbsolutePath()); System.out.print("文件目录:" + file2.getParent()); System.out.println("文件名称:" + file2.getName()); makeFileRecord(file2.getAbsolutePath()+"|"+file2.getParent()+"|"+file2.getName()); } } } } else { System.out.println("文件不存在!"); } } private static void makeFileRecord(String content) { BufferedWriter ous = null; try { File file = new File("/root/record.txt");//读取结果存入 txt if(!file.exists()) { file.createNewFile(); } // FileWriter(file,true) true 指定是以追加的方式读写。 ous = new BufferedWriter(new FileWriter(file, true)); ous.write(content); ous.newLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { ous.flush(); ous.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { folderMethod("/home/sse/RECOVERED_FILES/opt/apache-tomcat-7.0.57/webapps/EFRS/upload"); }
风雨无阻!!!

浙公网安备 33010602011771号