我想这是一个行读文件并解析的问题。
用readline的方法读文件,
判断开头是否为/* ,//这样的字符,如是,肯定为注释行,如不是,算作代码行,再判断是否含有/* ,//这样的字符,如是则也算作注释行,总共的行数为物理行。
简要说这些吧!祝你面试成功。
用readline的方法读文件,
判断开头是否为/* ,//这样的字符,如是,肯定为注释行,如不是,算作代码行,再判断是否含有/* ,//这样的字符,如是则也算作注释行,总共的行数为物理行。
简要说这些吧!祝你面试成功。
调试欢乐多
我刚刚完成这个软件的终极版。你的邮箱是多少。我发给你玩一下吧。哈哈哈
另外,既然完成了,就结贴吧^_^
[email protected]
谢先
private int files = 0;
private int size=0;
private int count = 0;
private int rem = 0;
private int code = 0;
private int empty = 0; public Counter(File f) {
count(f);
} private void count(File f) {
if (f.isFile()) {
countFile(f);
}
else {
File[] list = f.listFiles();
for (int i = 0; i < list.length; i++) {
count(list[i]);
}
}
} private void countFile(File f) {
if (f.getName().toLowerCase().endsWith(".java")) {
files++;
size+=f.length();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String s = null;
boolean isrem = false;
int k1,k2,k3,k4;
while ( (s = br.readLine()) != null) {
s = s.trim();
count++;
if(s.length()==0){
empty++;
continue;
}
k1=s.indexOf("/*");
k2=s.lastIndexOf("*/");
k3=s.indexOf("//");
if(isrem){
if(k2!=-1){
isrem=false;
if(k2!=s.length()-1){
code++;
}
}
rem++;
}
else{
if(k1==0){
isrem=true;
rem++;
continue;
}
else if(k1>0){
isrem=true;
rem++;
code++;
continue;
}
if(k3==0){
rem++;
continue;
}
else if(k3>0){
rem++;
code++;
continue;
}
code++;
}
}
}
catch (Exception ex) {ex.printStackTrace();}
}
} public static void main(String[] args) {
File f = new File(
"D:\\help");
Counter c = new Counter(f);
System.out.println("File :" + c.getFiles());
System.out.println("Size :" + c.getSize());
System.out.println("Count :" + c.getCount());
System.out.println("Rem :" + c.getRem());
System.out.println("Code :" + c.getCode());
System.out.println("Empty :" + c.getEmpty());
} public int getCode() {
return code;
} public int getCount() {
return count;
} public int getRem() {
return rem;
} public int getFiles() {
return files;
} public int getEmpty() {
return empty;
}
public int getSize() {
return size;
}}