- public static int getProjectFileNumber(File file, String endsWith) throws IOException {
- int number = 0;
- if (file.exists()) {
- if (file.isDirectory()) {
- for (File subFile : file.listFiles()) {
- number += getProjectFileNumber(subFile, endsWith);
- }
- } else if (file.isFile() && file.getName().endsWith(endsWith)) {
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- while (br.readLine() != null) {
- number += 1;
- }
- } else {
- System.out.println("===" + file.getAbsolutePath());
- }
- }
- return number;
- }
- public static void main(String[] args) throws IOException {
- //用法示例
- int num = Test.getProjectFileNumber(new File("D:\\Workspaces10\\web\\src\\com"), ".java");
- System.out.println(num);
- }
- //该片段来自于http://www.codesnippet.cn/detail/2503201511987.html
来源: http://www.codesnippet.cn/detail/2503201511987.html