楼主用正则,IO流写的一个统计代码单行注释 、多行注释、 空白行 、代码的行数的问题。但是还想有几个问题:
1、不能统计子目录下的情况,只能统计每一个java文件的行数说明和总的目录下的单行注释 、多行注释、 空白行 、代码的行数
2、输入错误的地址后,再输入正确的文件路径或者目录名。输出结果的总的统计的目录名会是所输入的第一错误的地址
3、输入中文会产生异常。
所以想请问一下,该怎么解决?谢谢。
----------------------------------------------------------以下是源代码------------------------------------------------------------------------------------------
1、不能统计子目录下的情况,只能统计每一个java文件的行数说明和总的目录下的单行注释 、多行注释、 空白行 、代码的行数
2、输入错误的地址后,再输入正确的文件路径或者目录名。输出结果的总的统计的目录名会是所输入的第一错误的地址
3、输入中文会产生异常。
所以想请问一下,该怎么解决?谢谢。
----------------------------------------------------------以下是源代码------------------------------------------------------------------------------------------
解决方案 »
- linux下安装mongodb 报错
- Why we use 31 in hashCode() method?
- ObjectInputStream如果从txt文件中读取2个java类?ObjectInputStream的实例也没有next方法呀?如何判断……
- 请问java的clone什么时候需要重写?
- JTable 能实现undo redo功能吗?
- 如何把java.math.BigDecimal类型后面多余的零(0)去掉?
- 高手帮忙啊!!!!!!!!!
- 如何隐藏单元格
- jbuilder中编译没有错误,可是无法运行。(我是初学者)
- 请问大家一个概念性地问题?
- 有个问题,想了很久,我怕世上没有多少人能做到
- Swing高手过来看看啊
package test;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;//vesion_3 输入目录或者文件名 输出每个目录下的单行注释 、多行注释、 空白行 、代码的行数和总的单行注释 、多行注释、 空白行 、代码的行数 very good
//主函数中调用判断目录和文件的函数,而不是先判断
//判断输入错误的情况
//保存统计内容
//有些瑕疵bug:1、不能统计子目录下的情况,只能统计每一个java文件的行数说明和总的目录下的单行注释 、多行注释、 空白行 、代码的行数
// 2、输入错误的地址后,再输入正确的文件路径或者目录名。输出结果的总的统计的目录名会是所输入的第一错误的地址
// 3、输入中文会产生异常
//vesion_4
public class CountCode4_SaveOptimize {
static int singleLines = 0;
static int multiLines = 0;
static int blankLines = 0;
static int codeLines = 0;
static boolean flag = false;
static int sum = 0;
static String s = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
static File saveFile = new File("h:\\countCode__" + new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + ".txt");
// static ArrayList<Integer> dirCount=new ArrayList<Integer>(); public static void main(String[] args) {
/*
* Calendar c=Calendar.getInstance();
* System.out.println(c.get(Calendar.YEAR));
* System.out.println(c.get(Calendar.MONTH)+1);
* System.out.println(c.get(Calendar.DATE));
*/ PrintWriter pwr;
try {
pwr = new PrintWriter(new FileWriter(saveFile, true));
pwr.println("------------------" + s + "------------------");
pwr.flush();
pwr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader brName = new BufferedReader(new InputStreamReader(System.in));
System.out.println(s + " 请输入所要统计的文件路径或目录路径:");
String fileName = null;
try {
fileName = brName.readLine();
} catch (IOException e) {
e.printStackTrace();
}
File f = new File(fileName);
confirmType(f);
try {
PrintWriter pw;
try {
pw = new PrintWriter(new FileWriter(saveFile,true)); System.out.println(f.getAbsolutePath() + " 目录下的合计统计数: ");
System.out.println(" 总单行注释: " + singleLines);
System.out.println(" 总多行注释: " + multiLines);
System.out.println(" 总空白行数: " + blankLines);
System.out.println(" 总代码行数: " + codeLines);
System.out.println(" 总java文件数: " + sum);
pw.println(f.getAbsolutePath() + " 目录下的合计统计数: ");
pw.println(" 总单行注释: " + singleLines);
pw.println(" 总多行注释: " + multiLines);
pw.println(" 总空白行数: " + blankLines);
pw.println(" 总代码行数: " + codeLines);
pw.println(" 总java文件数: " + sum); pw.flush();
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void confirmType(File f) {
if (f.isDirectory()) {
System.out.println(f.getAbsolutePath()+" 目录下java文件说明表--------");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(f.getAbsolutePath()+" 目录下java文件说明表--------" + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
beginDir(f);
} else if (f.isFile()) {
countCode(f);
} else {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss "); System.out.println(sdf.format(d) + " 输入错误,请重新输入"); BufferedReader brName = new BufferedReader(new InputStreamReader(System.in));
try {
File fi = new File(brName.readLine());
confirmType(fi);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} // single Multi blank code line
private static void beginDir(File dir) {
/*int single = 0;
int multi = 0;
int blank = 0;
int code = 0;*/
int dirFile=0;
// TODO Auto-generated method stub
File[] files = dir.listFiles();
for (File child : files) {
if (child.isDirectory()) {
System.out.println(child.getAbsolutePath()+" 目录下java文件说明表--------");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getAbsolutePath()+" 目录下java文件说明表--------" + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
beginDir(child); } else if (child.getName().matches(".*\\.java$")) {//正则表达式,以.java结尾的文件
countCode(child);
dirFile++;
}else {
System.out.println(child.getAbsolutePath() + " 不是java源代码文件"+ "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getAbsolutePath() + " 不是java源代码文件"+ "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*Iterator<Integer> ite=dirCount.iterator();
while(ite.hasNext()){
single=ite.next();
multi=ite.next();
blank=ite.next();
code=ite.next();
}*/
System.out.println(dir.getName()+" 目录下的java文件数:"+dirFile);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(dir.getName()+" 目录下的java文件数:"+dirFile+ "\r\n");
/*System.out.println(dir.getName()+" 单行注释:" + single);
System.out.println(dir.getName()+" 多行注释:" + multi);
System.out.println(dir.getName()+" 空白行数:" + blank);
System.out.println(dir.getName()+" 代码行数:" + code);*/
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void countCode(File child) {
int single = 0;
int multi = 0;
int blank = 0;
int code = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(child.getAbsoluteFile()))); String line = null;
try {
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches(".*//.*")) {
single++;
singleLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
multi++;
multiLines++;
flag = true;
} else if (true == flag && !line.endsWith("*/")) {
multi++;
multiLines++;
} else if (line.endsWith("*/")) {
multi++;
multiLines++;
flag = false;
} else if (line.matches("^[\\s&&[^\\n]]*$")) {
// rendline会取消换行符号,所以不是 "^[\\s&&[^\\n]]*\\n$"
// 以\n结尾
blank++;
blankLines++;
} else {
code++;
codeLines++;
}
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println(child.getName() + " 统计:");
System.out.println(" 单行注释:" + single);
System.out.println(" 多行注释:" + multi);
System.out.println(" 空白行数:" + blank);
System.out.println(" 代码行数:" + code);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getName() + " 统计数:" + "\r\n");
bw.write(" 单行注释:" + single + "\r\n");
bw.write(" 多行注释:" + multi + "\r\n");
bw.write(" 空白行数:" + blank + "\r\n");
bw.write(" 代码行数:" + code + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sum++;
// dirCount.add(single);
// dirCount.add(multi);
// dirCount.add(blank);
// dirCount.add(code);
}}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;//vesion_3 输入目录或者文件名 输出每个目录下的单行注释 、多行注释、 空白行 、代码的行数和总的单行注释 、多行注释、 空白行 、代码的行数 very good
//主函数中调用判断目录和文件的函数,而不是先判断
//判断输入错误的情况
//保存统计内容
//有些瑕疵bug:1、不能统计子目录下的情况,只能统计每一个java文件的行数说明和总的目录下的单行注释 、多行注释、 空白行 、代码的行数
// 2、输入错误的地址后,再输入正确的文件路径或者目录名。输出结果的总的统计的目录名会是所输入的第一错误的地址
// 3、输入中文会产生异常
//vesion_4
public class CountCode4_SaveOptimize {
static int singleLines = 0;
static int multiLines = 0;
static int blankLines = 0;
static int codeLines = 0;
static boolean flag = false;
static int sum = 0;
static String s = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
static File saveFile = new File("h:\\countCode__" + new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + ".txt");
// static ArrayList<Integer> dirCount=new ArrayList<Integer>(); public static void main(String[] args) {
/*
* Calendar c=Calendar.getInstance();
* System.out.println(c.get(Calendar.YEAR));
* System.out.println(c.get(Calendar.MONTH)+1);
* System.out.println(c.get(Calendar.DATE));
*/ PrintWriter pwr;
try {
pwr = new PrintWriter(new FileWriter(saveFile, true));
pwr.println("------------------" + s + "------------------");
pwr.flush();
pwr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader brName = new BufferedReader(new InputStreamReader(System.in));
System.out.println(s + " 请输入所要统计的文件路径或目录路径:");
String fileName = null;
try {
fileName = brName.readLine();
} catch (IOException e) {
e.printStackTrace();
}
File f = new File(fileName);
confirmType(f);
try {
PrintWriter pw;
try {
pw = new PrintWriter(new FileWriter(saveFile,true)); System.out.println(f.getAbsolutePath() + " 目录下的合计统计数: ");
System.out.println(" 总单行注释: " + singleLines);
System.out.println(" 总多行注释: " + multiLines);
System.out.println(" 总空白行数: " + blankLines);
System.out.println(" 总代码行数: " + codeLines);
System.out.println(" 总java文件数: " + sum);
pw.println(f.getAbsolutePath() + " 目录下的合计统计数: ");
pw.println(" 总单行注释: " + singleLines);
pw.println(" 总多行注释: " + multiLines);
pw.println(" 总空白行数: " + blankLines);
pw.println(" 总代码行数: " + codeLines);
pw.println(" 总java文件数: " + sum); pw.flush();
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void confirmType(File f) {
if (f.isDirectory()) {
System.out.println(f.getAbsolutePath()+" 目录下java文件说明表--------");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(f.getAbsolutePath()+" 目录下java文件说明表--------" + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
beginDir(f);
} else if (f.isFile()) {
countCode(f);
} else {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss "); System.out.println(sdf.format(d) + " 输入错误,请重新输入"); BufferedReader brName = new BufferedReader(new InputStreamReader(System.in));
try {
File fi = new File(brName.readLine());
confirmType(fi);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} // single Multi blank code line
private static void beginDir(File dir) {
/*int single = 0;
int multi = 0;
int blank = 0;
int code = 0;*/
int dirFile=0;
// TODO Auto-generated method stub
File[] files = dir.listFiles();
for (File child : files) {
if (child.isDirectory()) {
System.out.println(child.getAbsolutePath()+" 目录下java文件说明表--------");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getAbsolutePath()+" 目录下java文件说明表--------" + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
beginDir(child); } else if (child.getName().matches(".*\\.java$")) {//正则表达式,以.java结尾的文件
countCode(child);
dirFile++;
}else {
System.out.println(child.getAbsolutePath() + " 不是java源代码文件"+ "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getAbsolutePath() + " 不是java源代码文件"+ "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*Iterator<Integer> ite=dirCount.iterator();
while(ite.hasNext()){
single=ite.next();
multi=ite.next();
blank=ite.next();
code=ite.next();
}*/
System.out.println(dir.getName()+" 目录下的java文件数:"+dirFile);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(dir.getName()+" 目录下的java文件数:"+dirFile+ "\r\n");
/*System.out.println(dir.getName()+" 单行注释:" + single);
System.out.println(dir.getName()+" 多行注释:" + multi);
System.out.println(dir.getName()+" 空白行数:" + blank);
System.out.println(dir.getName()+" 代码行数:" + code);*/
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void countCode(File child) {
int single = 0;
int multi = 0;
int blank = 0;
int code = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(child.getAbsoluteFile()))); String line = null;
try {
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches(".*//.*")) {
single++;
singleLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
multi++;
multiLines++;
flag = true;
} else if (true == flag && !line.endsWith("*/")) {
multi++;
multiLines++;
} else if (line.endsWith("*/")) {
multi++;
multiLines++;
flag = false;
} else if (line.matches("^[\\s&&[^\\n]]*$")) {
// rendline会取消换行符号,所以不是 "^[\\s&&[^\\n]]*\\n$"
// 以\n结尾
blank++;
blankLines++;
} else {
code++;
codeLines++;
}
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println(child.getName() + " 统计:");
System.out.println(" 单行注释:" + single);
System.out.println(" 多行注释:" + multi);
System.out.println(" 空白行数:" + blank);
System.out.println(" 代码行数:" + code);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile, true));
bw.write(child.getName() + " 统计数:" + "\r\n");
bw.write(" 单行注释:" + single + "\r\n");
bw.write(" 多行注释:" + multi + "\r\n");
bw.write(" 空白行数:" + blank + "\r\n");
bw.write(" 代码行数:" + code + "\r\n");
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sum++;
// dirCount.add(single);
// dirCount.add(multi);
// dirCount.add(blank);
// dirCount.add(code);
}}