重要提示:.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;
public class Bin {
public static void main ( String[ ] args) throws FileNotFoundException {
String path = "D:/JavaFiles/files/Hello.java" ;
BufferedReader br = new BufferedReader ( new FileReader ( path) ) ;
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 {
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 ( ) ) {
if ( map. get ( k) < 3 )
continue ;
System. out. printf ( "%s = %d\n" , k, map. get ( k) ) ;
++ num;
}
System. out. println ( "输出的字符中类数:" + num) ;
}
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;
}
}
}