Ⅰ java統計類中物理總行數中注釋行,非注釋行,以及代碼的行數
import java.io.*;public class Check {
public static void main (String[] args) throws IOException{
check("D:/MyEclipse/java/src/my/Check.java");//這里的字元串是你要統計的文件的路徑,你自己填寫
} public static void check (String s) throws IOException{
int all = 0, empty = 0, describe = -1, i = 0;
String str = null;
File f = new File(s);
BufferedReader br = new BufferedReader (new FileReader(f));
str = br.readLine();
while(str != null){
all++;
if(str.trim().equals("")) empty++;
if(str.contains("//")) describe++;
if(str.contains("/*")){
while(!str.contains("*/")){
i++;
all++;
describe++;
str = br.readLine();
}
}
str = br.readLine();
}
System.out.println("文件物理總行數為:" + all);//;;klj
System.out.println("文件中空行數為:" + empty);//hkk
System.out.println("文件注釋行數為:" + describe);
System.out.println("文件非注釋行數為:" + (all - i));
/*asdfdsff
* sdasadfsf//fg
* asdfsdf//dsfg
* asdf
*/
}
}以上是代碼,我在我的機子上實現了,希望能幫到你!我也是JAVA菜鳥,希望有高手能更好地解答
Ⅱ eclipse怎麼統計代碼行數
步驟如下:
1、打開File Search對話框。
2、選中正則表達式,在搜索文本框輸入\n 。
3、文件名稱輸入 *.java。
4、在范圍里選中Enclosing projects。
經過上面方式,就可以統計出整個項目的代碼行數。
Ⅲ 求問怎麼統計JAVA代碼行數有什麼工具
源代碼行數統計器 1.5
本軟體用於統計軟體工程源代碼行數,
可對指定的子目錄下或整個目錄樹中所有指定類型的源代碼文件進行行數統計。
Ⅳ 如何統計某目錄下的java文件代碼行數
可以自己寫一個小程序,遍歷每個文件;
如果是*.java就記錄該文件的行數,依次累加。
Ⅳ Java 有什麼好的代碼行數,注釋行數統計工具
package com.syl.demo.test;
import java.io.*;
/**
* java代碼行數統計工具類
* Created by 孫義朗 on 2017/11/17 0017.
*/
public class CountCodeLineUtil {
private static int normalLines = 0; //有效程序行數
private static int whiteLines = 0; //空白行數
private static int commentLines = 0; //注釋行數
public static void countCodeLine(File file) {
System.out.println("代碼行數統計:" + file.getAbsolutePath());
if (file.exists()) {
try {
scanFile(file);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件不存在!");
System.exit(0);
}
System.out.println(file.getAbsolutePath() + " ,java文件統計:" +
"總有效代碼行數: " + normalLines +
" ,總空白行數:" + whiteLines +
" ,總注釋行數:" + commentLines +
" ,總行數:" + (normalLines + whiteLines + commentLines));
}
private static void scanFile(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
scanFile(files[i]);
}
}
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
count(file);
}
}
}
private static void count(File file) {
BufferedReader br = null;
// 判斷此行是否為注釋行
boolean comment = false;
int temp_whiteLines = 0;
int temp_commentLines = 0;
int temp_normalLines = 0;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[//s&&[^//n]]*$")) {
// 空行
whiteLines++;
temp_whiteLines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
// 判斷此行為"/*"開頭的注釋行
commentLines++;
comment = true;
} else if (comment == true && !line.endsWith("*/")) {
// 為多行注釋中的一行(不是開頭和結尾)
commentLines++;
temp_commentLines++;
} else if (comment == true && line.endsWith("*/")) {
// 為多行注釋的結束行
commentLines++;
temp_commentLines++;
comment = false;
} else if (line.startsWith("//")) {
// 單行注釋行
commentLines++;
temp_commentLines++;
} else {
// 正常代碼行
normalLines++;
temp_normalLines++;
}
}
System.out.println(file.getName() +
" ,有效行數" + temp_normalLines +
" ,空白行數" + temp_whiteLines +
" ,注釋行數" + temp_commentLines +
" ,總行數" + (temp_normalLines + temp_whiteLines + temp_commentLines));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//測試
public static void main(String[] args) {
File file = new File("F:\\myweb");
countCodeLine(file);
}
}
Ⅵ 輸入:1個目錄 輸出:這個目錄下的java文件的代碼行數總和(統計時忽視注釋和空行),
//統計程序生成文件的行數
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("行數統計完畢,請查看。");
}
}
差不多,自己改改吧,我這個不能忽略注釋和空行。
Ⅶ 如何計算一個.java文件的代碼行數
方法一:
如果想要通過java代碼的方式來計算.java文件的行數,可以通過IO來讀取,
BufferedReader的方法readLine()來按行讀取,每讀取一行,行數+1
方法二:
如果要查看.java文件的代碼行數,
可以使用現成的IDE工具,比如ECLIPSE...
每一行的行號都有表示出來
Ⅷ 怎麼用java編寫統計文件中的字元數、單詞數和行數
在C盤新建文件1.txt,輸入任意字元,如下圖:
Ⅸ java中怎麼獲得一個文本文件的行數
涉及到java中讀寫文件的IO操作。
獲取一個文本文件的行數較為方便的方法,是通過BufferedReader類的readLine()方法,間接的統計行數。
源代碼:
public
static
int
getTextLines()
throws
IOException
{
String
path
=
"c:\\job.txt"
;//
定義文件路徑
FileReader
fr
=
new
FileReader(path);
//這里定義一個字元流的輸入流的節點流,用於讀取文件(一個字元一個字元的讀取)
BufferedReader
br
=
new
BufferedReader(fr);
//
在定義好的流基礎上套接一個處理流,用於更加效率的讀取文件(一行一行的讀取)
int
x
=
0;
//
用於統計行數,從0開始
while(br.readLine()
!=
null)
{
//
readLine()方法是按行讀的,返回值是這行的內容
x++;
//
每讀一行,則變數x累加1
}
return
x;
//返回總的行數
}