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 } } } } } }
扫描了”E:\\JavaWorkSpace\\Workspaces\\eclipse“中所有的.java文件,并进行了文件的读取和输出
时间: 2024-10-06 08:23:06