111
作者:
wzx_0
,
2023-01-06 16:42:19
,
所有人可见
,
阅读 161
package com.jf;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class directory {
static int sumfiles = 0;
static int sumlines = 0;
static int sumblanks = 0;
static int sumbytes = 0;
public static long getbytes(File file) {
return file.length();
}
public static int getlines(File path) throws IOException {
int res=0;
BufferedReader br=new BufferedReader(new FileReader(path));
String line;
while((line= br.readLine())!=null){
res++;
}
return res;
}
public static int getblanks(File path) throws IOException {
int res=0;
BufferedReader br=new BufferedReader(new FileReader(path));
String line;
while((line= br.readLine())!=null){
if(line.equals("")) res++;
}
return res;
}
static void dfs(File path, int cnt, FileWriter fw) throws IOException {
for(int i=1;i<=cnt*4;i++) System.out.print(" ");
if (isjava(path)) {
System.out.println("-"+path.getName());
sumfiles++;
int lines=getlines(path);
sumlines+=lines;
int blanks=getblanks(path);
sumblanks+=blanks;
long bytes=getbytes(path);
sumbytes+=bytes;
System.out.println("\tTotal:\t" + String.format("%5d", lines) + ", Blank:\t"
+ String.format("%5d",blanks) + ",\t" + String.format("%5d", bytes)
+ " Bytes");
return;
}
File[] files = path.listFiles();
sortpaths(files);
System.out.println("+"+path.getName());
for (int i = 0; i < files.length; i++) {
dfs(files[i], cnt + 1, fw);
}
}
public static boolean isjava(File file){
String temp=file.toString();
return temp.endsWith(".java");
}
public static void sortpaths(File[] files){
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1,File o2){
if (o1.isFile() && o2.isFile()) {
return o1.getName().compareTo(o2.getName());
} else if (o1.isDirectory() && o2.isDirectory()) {
return o1.getName().compareTo(o2.getName());
} else if (o1.isDirectory() && o2.isFile()) {
return -1;
} else if (o1.isFile() && o2.isDirectory()) {
return 1;
}
return 0;
}
class work{
public static void write(File path) throws IOException {
Boolean success=new File(path+"\\result").mkdir();
File temp=new File(path+"\\result"+"\\"+path.getName()+".txt");
boolean fr=temp.createNewFile();
FileWriter fw=new FileWriter(temp);
fw.write("["+path.getAbsolutePath()+"]"+"Result:\n\n");
fw.write("File details:\n");
directory.dfs(path,0,fw);
fw.close();
}
}
public class Main {
public static void show() {
System.out.println("-----MENU-----");
System.out.println("1.分析目录中的源程序文件");
System.out.println("2.查看分析结果");
System.out.println("0.退出程序");
System.out.println("--------------");
System.out.println("请选择:");
}
public static void main(String[] args) throws IOException {
show();
Scanner sc = new Scanner(System.in);
int op = 1;
if (op == 1) {
System.out.println("请输入目录名称");
String dir = "D:\\test";
File path = new File(dir);
if (path.isDirectory()) {
work.write(path);
} else {
System.out.println("错误:[" + dir + "]" + "不是目录名或不存在");
}
}
}
}