输入:1个目录 输出:这个目录下的java文件的代码行数总和(统计时忽视注释和空行),
1个回答
展开全部
//统计程序生成文件的行数
package package_1;
import java.io.*;
public class Linknum {
File tempFile1,tempFile2;
File tempFiles[] = null;
String path,temp = "";
int num = 2;
public Linknum() throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + "\t" + temp + temp2 + "\r\n" + num + "\t";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + "\t";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(path,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public Linknum(String s)throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + ". " + temp + temp2 + "\r\n" + num + ". ";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + ". ";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(s,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入保存文件的路径(或输入空格默认):");
String str = br.readLine();
br.close();
if(str.equals(" "))
new Linknum();
else
new Linknum(str);
System.out.println("行数统计完毕,请查看。");
}
}
差不多,自己改改吧,我这个不能忽略注释和空行。
package package_1;
import java.io.*;
public class Linknum {
File tempFile1,tempFile2;
File tempFiles[] = null;
String path,temp = "";
int num = 2;
public Linknum() throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + "\t" + temp + temp2 + "\r\n" + num + "\t";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + "\t";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(path,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public Linknum(String s)throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + ". " + temp + temp2 + "\r\n" + num + ". ";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + ". ";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(s,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入保存文件的路径(或输入空格默认):");
String str = br.readLine();
br.close();
if(str.equals(" "))
new Linknum();
else
new Linknum(str);
System.out.println("行数统计完毕,请查看。");
}
}
差不多,自己改改吧,我这个不能忽略注释和空行。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询