java輸出某資料夾或某檔案的個數和檔案的行數及檔案包含的字元種類數和次數
阿新 • • 發佈:2021-01-20
目錄
- 重要提示:~~.java~~ 只是舉例,同理可換成別的
- 統計某目錄下的.java檔案個數
- 統計某個.java檔案的行數
- 統計某目錄下.java檔案個數和某目錄的所有.java檔案的總行數
- 輸出某個.java檔案中各字元出現的次數
- 輸出某資料夾的所有.java檔案裡字元出現的次數
重要提示:.java 只是舉例,同理可換成別的
統計某目錄下的.java檔案個數
public class Bin3 { /** * 統計某目錄下的.java檔案個數 */ static int num; public static void main(String[] args) { count(new File("D:/JavaFiles/files/train")); System.out.println(num); } public static void count(File dir) { if (dir.isDirectory()) { File[] f = dir.listFiles(); for (File t : f) { if (t.isDirectory()) { count(t); } if (t.isFile() && t.getName().endsWith(".java")) { ++num; } } } } }
統計某個.java檔案的行數
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* 統計某個.java檔案的行數
*/
public class Bin {
public static void main(String[] args) throws FileNotFoundException {
// 讀取單個.java檔案的行數
String path = "D:/JavaFiles/files/Hello.java";
BufferedReader br = new BufferedReader(new FileReader(path));
// System.out.println("總行數(包括空行):"+br.lines().count());
System.out.println("排除空行後的行數:" + br.lines().filter(e -> e.trim().length() > 0).count());
}
}
統計某目錄下.java檔案個數和某目錄的所有.java檔案的總行數
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; public class Bin4 { /** * 統計某目錄下.java檔案個數和某目錄的所有.java檔案的總行數 * */ static int num; static int rows; public static void main(String[] args) throws Exception{ count(new File("D:/JavaFiles/files/jiujiu")); System.out.println("檔案個數:"+num); System.out.println("檔案總行數"+rows); } public static void count(File dir) throws FileNotFoundException { if(dir.isDirectory()) { File[] f=dir.listFiles(); for(File t:f) { if(t.isDirectory()) { count(t); }if(t.isFile() && t.getName().endsWith(".java")) { ++num; BufferedReader br=new BufferedReader(new FileReader(t)); rows+=br.lines().count(); } } } } }
輸出某個.java檔案中各字元出現的次數
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 輸出某個.java檔案中各字元出現的次數
*/
public class JavaDemo2 {
public static void main(String[] args) throws FileNotFoundException {
Pattern p = Pattern.compile("[a-zA-Z]+");
Map<String, Integer> map = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("D:/JavaFiles/files/jiujiu/jiujiu.java"));
br.lines().forEach(e -> {
if (e.trim().length() > 0) {
Matcher m = p.matcher(e);
while (m.find()) {
String w = m.group();
if (map.containsKey(w)) {
map.put(w, map.get(w) + 1);
} else {
map.put(w, 1);
}
}
}
});
System.out.println("該.java檔案字元種類數:" + map.size());
for (String k : map.keySet()) {
System.out.printf("%s = %d\n", k, map.get(k));
}
}
}
輸出某資料夾的所有.java檔案裡字元出現的次數
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaDemo3 {
/**
* 輸出某資料夾的所有.java檔案裡字元出現的次數
*
*/
static int num = 0;
static Map<String, Integer> map = new HashMap<>();
public static void main(String[] args) throws FileNotFoundException {
File file = new File("D:/JavaFiles/files/1");
count(file);
System.out.println("該資料夾字元種類數:" + map.size());
for (String k : map.keySet()) {
System.out.printf("%s = %d\n", k, map.get(k));
}
}
private static void count(File file) throws FileNotFoundException {
Pattern p = Pattern.compile("[a-zA-Z]+");
if (file.isDirectory()) {
File[] f = file.listFiles();
for (File nn : f) {
if (nn.isDirectory()) {
count(nn);
}
if (nn.getName().endsWith(".java")) {
BufferedReader br = null;
br = new BufferedReader(new FileReader(nn));
br.lines().forEach(e -> {
if (e.trim().length() > 0) {
Matcher m = p.matcher(e);
while (m.find()) {
String w = m.group();
if (map.containsKey(w)) {
map.put(w, map.get(w) + 1);
} else {
map.put(w, 1);
}
}
}
});
}
}
}
if (file.getName().endsWith(".java")) {
++num;
}
}
}