import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class test { static int normaline = 0;
static int commentline = 0;
static int whiteline = 0;
public static void main(String arg[]){
File f = new File("E:\\javaworkspace\\noname\\src\\sdf");
File[] list = f.listFiles(); for(File shit : list)
{
if(shit.getName().matches(".*\\.java$"));
{
tongji(shit);
}
}
System.out.println("正常的代码行数"+normaline);
System.out.println("注释的行数"+commentline);
System.out.println("空白的行数"+whiteline);
} private static void tongji(File f) {
BufferedReader bf = null;
boolean comment= false;
try {
bf = new BufferedReader(new FileReader(f));
String line = "";
while((line=bf.readLine())!= null)
{
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteline++;
}else if(line.startsWith("/*")&&!line.endsWith("*/")){
commentline++;
comment = true;
}else if(line.startsWith("//"))
{
commentline++;
}else if (comment == true){
commentline++;
if(line.endsWith("*/")){
comment = false;
}
}else {
normaline++;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} if(shit.getName().matches(".*\\.java$"));
{
tongji(shit);
}
这里如果换成了while就不能显示了???为什么讷讷?????求大神解答
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class test { static int normaline = 0;
static int commentline = 0;
static int whiteline = 0;
public static void main(String arg[]){
File f = new File("E:\\javaworkspace\\noname\\src\\sdf");
File[] list = f.listFiles(); for(File shit : list)
{
if(shit.getName().matches(".*\\.java$"));
{
tongji(shit);
}
}
System.out.println("正常的代码行数"+normaline);
System.out.println("注释的行数"+commentline);
System.out.println("空白的行数"+whiteline);
} private static void tongji(File f) {
BufferedReader bf = null;
boolean comment= false;
try {
bf = new BufferedReader(new FileReader(f));
String line = "";
while((line=bf.readLine())!= null)
{
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteline++;
}else if(line.startsWith("/*")&&!line.endsWith("*/")){
commentline++;
comment = true;
}else if(line.startsWith("//"))
{
commentline++;
}else if (comment == true){
commentline++;
if(line.endsWith("*/")){
comment = false;
}
}else {
normaline++;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} if(shit.getName().matches(".*\\.java$"));
{
tongji(shit);
}
这里如果换成了while就不能显示了???为什么讷讷?????求大神解答
解决方案 »
- jboss是用来干嘛的??????????????
- JS跨域调用
- j2ee发展规划
- struts2为什么不显示结果
- <s:action name="actionTagAction" executeResult="true" /> Tag
- 写出满足要求的SQL语句
- 如何从xml里读取参数
- 请问Eclipse ,tomcat,jboss等软件是用JAVA写的吗?都运行在JVM之上吗?
- Applet与数据库与Servlet的问题,分数不够可以再加
- AnnoationConfiguration cannot be resolved to a type
- 求大牛指点迷津(测试题)
- 新手编写一个新的java程序,求高手指教
比如你的第一个shit文件名就是以.java结尾的。
也就是shit.getName().matches(".*\\.java$") == true
而用while会死循环吧
如果想用while 把 for 替换掉不就行了