Created
November 27, 2018 09:17
-
-
Save sunzcdev/5d4a621adf338dd3095bde467e39c360 to your computer and use it in GitHub Desktop.
统计指定目录下的文本行数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileFilter; | |
import java.io.FileReader; | |
import java.io.IOException; | |
/** | |
* @author Administrator | |
* @date 2018-11-27 16:42 | |
**/ | |
public class Count { | |
public static void main(String[] args) { | |
long java = count(new File("D:\\App"), new FileFilter() { | |
@Override | |
public boolean accept(File pathname) { | |
return !pathname.getAbsolutePath().contains("D:\\App\\.idea") | |
&& !pathname.getAbsolutePath().contains("D:\\App\\gen") | |
&& (pathname.isDirectory() | |
|| pathname.getName().endsWith(".java") | |
|| pathname.getName().endsWith(".xml")); | |
} | |
}); | |
System.out.println(java); | |
} | |
public static long count(File dir, FileFilter filter) { | |
if (!dir.exists()) return 0; | |
if (dir.isDirectory()) { | |
File[] files = dir.listFiles(filter); | |
if (files.length <= 0) { | |
return 0; | |
} else { | |
int length = 0; | |
for (File file : files) { | |
length += count(file, filter); | |
} | |
return length; | |
} | |
} else if (dir.isFile()) { | |
System.out.println(dir); | |
int length = 0; | |
try { | |
BufferedReader r = new BufferedReader(new FileReader(dir)); | |
while (null != r.readLine()) { | |
length++; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return length; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment