时间:2020-10-27 12:17:57 | 栏目:JAVA代码 | 点击:次
具体代码如下所示:
import java.io.File; public class Scan { public static void main(String[] args) { String fileName = "D:\\Program Files\\腾讯游戏\\英雄联盟" + File.separator; File f = new File(fileName); scan(f); } public static void scan(File f) { if (f != null) { if (f.isDirectory()) { File[] fileArray = f.listFiles(); if (fileArray != null) { for (int i = 0; i < fileArray.length; i++) { // 递归调用 scan(fileArray[i]); } } } else { System.out.println(f); } } } }
扫描了“D:\\Program Files\\腾讯游戏\\英雄联盟”中的所有文件
而下面这个可以进行文件的扫描和读取
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; public class TextReader { public static void main(String[] args) { String fileName = "E:\\JavaWorkSpace\\Workspaces\\eclipse" + File.separator; File f = new File(fileName); scan(f); } public static void scan(File f) { if (f != null) { if (f.isDirectory()) { File[] fileArray = f.listFiles(); if (fileArray != null) { for (int i = 0; i < fileArray.length; i++) { // 递归调用 scan(fileArray[i]); } } } else { if ((f.getName().substring(f.getName().lastIndexOf(".") + 1, f .getName().length())).equals("java")) { FileReader reader; try { Thread.sleep(3000); reader = new FileReader(f); BufferedReader Bufferedreader = new BufferedReader( reader); String content = ""; while ((content = Bufferedreader.readLine()) != null) { System.out.println(content); } } catch (Exception e) { // TODO Auto-generated catch block } } } } } }
总结