方法一:

創(chuàng)新互聯(lián)公司服務(wù)項目包括平塘網(wǎng)站建設(shè)、平塘網(wǎng)站制作、平塘網(wǎng)頁制作以及平塘網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,平塘網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到平塘省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
如果想要通過java代碼的方式來計算.java文件的行數(shù),可以通過IO來讀取,
BufferedReader的方法readLine()來按行讀取,每讀取一行,行數(shù)+1
方法二:
如果要查看.java文件的代碼行數(shù),
可以使用現(xiàn)成的IDE工具,比如ECLIPSE...
每一行的行號都有表示出來
步驟如下:
1、打開File Search對話框。
2、選中正則表達式,在搜索文本框輸入\n 。
3、文件名稱輸入 *.java。
4、在范圍里選中Enclosing projects。
經(jīng)過上面方式,就可以統(tǒng)計出整個項目的代碼行數(shù)。
package com.syl.demo.test;
import java.io.*;
/**
* java代碼行數(shù)統(tǒng)計工具類
* Created by 孫義朗 on 2017/11/17 0017.
*/
public class CountCodeLineUtil {
private static int normalLines = 0; //有效程序行數(shù)
private static int whiteLines = 0; //空白行數(shù)
private static int commentLines = 0; //注釋行數(shù)
public static void countCodeLine(File file) {
System.out.println("代碼行數(shù)統(tǒng)計:" + 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文件統(tǒng)計:" +
"總有效代碼行數(shù): " + normalLines +
" ,總空白行數(shù):" + whiteLines +
" ,總注釋行數(shù):" + commentLines +
" ,總行數(shù):" + (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("*/")) {
// 為多行注釋中的一行(不是開頭和結(jié)尾)
commentLines++;
temp_commentLines++;
} else if (comment == true line.endsWith("*/")) {
// 為多行注釋的結(jié)束行
commentLines++;
temp_commentLines++;
comment = false;
} else if (line.startsWith("http://")) {
// 單行注釋行
commentLines++;
temp_commentLines++;
} else {
// 正常代碼行
normalLines++;
temp_normalLines++;
}
}
System.out.println(file.getName() +
" ,有效行數(shù)" + temp_normalLines +
" ,空白行數(shù)" + temp_whiteLines +
" ,注釋行數(shù)" + temp_commentLines +
" ,總行數(shù)" + (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);
}
}
計算全部代碼行數(shù)方法。
方法一:在需要統(tǒng)計的目錄下執(zhí)行(直接復(fù)制可能會有字符編碼問題,建議手?jǐn)],如需統(tǒng)計其他形式文件行數(shù),只需修改.java為相應(yīng)文件后綴即可)
find.-name*.java-execwc-l{};|awk‘{s+=$1}END{prints}’
方法二:l/傳入文件路徑返回文件下所有文件內(nèi)容行數(shù)unsignedlongcodeLineCount(NSString*Path)
//獲得文件管理者
NSFileManager*mgr=[NSFileManagerdefaultManager];
//判path是文件夾還是文件路徑
BoOLdir=No;//標(biāo)記是否為文件夾
l/這個路徑是否存在
BoOLexists=[mgrfileExistsAtPath:PathisDirectory:dir];
//如果不存在,直接返回e
if(!exists)returne;
if(dir){
NSLog(@"是個文件夾");
在C盤新建文件1.txt,輸入任意字符,如下圖:
編寫java代碼。如下:
import?java.io.BufferedReader;
import?java.io.FileNotFoundException;
import?java.io.FileReader;
import?java.io.IOException;
import?java.util.TreeMap;
public?class?Test?{
//?統(tǒng)計數(shù)字或者字符出現(xiàn)的次數(shù)
public?static?TreeMapCharacter,?Integer?Pross(String?str)?{
char[]?charArray?=?str.toCharArray();
TreeMapCharacter,?Integer?tm?=?new?TreeMapCharacter,?Integer();
for?(int?x?=?0;?x??charArray.length;?x++)?{
if?(!tm.containsKey(charArray[x]))?{
tm.put(charArray[x],?1);
}?else?{
int?count?=?tm.get(charArray[x])?+?1;
tm.put(charArray[x],?count);
}
}
return?tm;
}
public?static?void?main(String[]?args)?{
BufferedReader?br?=?null;
int?line?=?0;
String?str?=?"";
StringBuffer?sb??=?new?StringBuffer();
try?{
br?=?new?BufferedReader(new?FileReader("c:\\1.txt"));
while?((str?=?br.readLine())?!=?null)?{
sb.append(str);
++line;
}
System.out.println("\n文件行數(shù):?"?+?line);
System.out.println("\n文件內(nèi)容:?"?+?sb.toString());
TreeMapCharacter,?Integer?tm?=?Pross(sb.toString());
System.out.println("\n字符統(tǒng)計結(jié)果為:"?+?tm);
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
if?(br?!=?null)?{
try?{
br.close();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
}
}運行結(jié)果如下圖:
網(wǎng)站題目:java代碼行數(shù)統(tǒng)計,java代碼行數(shù)統(tǒng)計工具
URL地址:http://chinadenli.net/article38/dsshcsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、、企業(yè)建站、全網(wǎng)營銷推廣、外貿(mào)建站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)