獲取行數(shù)涉及到j(luò)ava中讀寫(xiě)文件的IO操作。
“真誠(chéng)服務(wù),讓網(wǎng)絡(luò)創(chuàng)造價(jià)值”是我們的服務(wù)理念,創(chuàng)新互聯(lián)建站團(tuán)隊(duì)十多年如一日始終堅(jiān)持在網(wǎng)站建設(shè)領(lǐng)域,為客戶提供優(yōu)質(zhì)服。不管你處于什么行業(yè),助你輕松跨入“互聯(lián)網(wǎng)+”時(shí)代,PC網(wǎng)站+手機(jī)網(wǎng)站+公眾號(hào)+微信小程序開(kāi)發(fā)。
獲取一個(gè)文本文件的行數(shù)較為方便的方法,是通過(guò)BufferedReader類的readLine()方法,間接的統(tǒng)計(jì)行數(shù)。
源代碼:
public static int getTextLines() throws IOException {
String path = "c:\\job.txt" ;// 定義文件路徑
FileReader fr = new FileReader(path); //這里定義一個(gè)字符流的輸入流的節(jié)點(diǎn)流,用于讀取文件(一個(gè)字符一個(gè)字符的讀?。?/p>
BufferedReader br = new BufferedReader(fr); // 在定義好的流基礎(chǔ)上套接一個(gè)處理流,用于更加效率的讀取文件(一行一行的讀?。?/p>
int x = 0; // 用于統(tǒng)計(jì)行數(shù),從0開(kāi)始
while(br.readLine() != null) { // readLine()方法是按行讀的,返回值是這行的內(nèi)容
x++; // 每讀一行,則變量x累加1
}
return x; //返回總的行數(shù)
}
相信看完上面的,應(yīng)該就會(huì)了。
可以自己寫(xiě)一個(gè)小程序,遍歷每個(gè)文件;
如果是*.java就記錄該文件的行數(shù),依次累加。
package com.syl.demo.test;
import java.io.*;
/**
* java代碼行數(shù)統(tǒng)計(jì)工具類
* 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)計(jì):" + 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)計(jì):" +
"總有效代碼行數(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("*/")) {
// 判斷此行為"/*"開(kāi)頭的注釋行
commentLines++;
comment = true;
} else if (comment == true !line.endsWith("*/")) {
// 為多行注釋中的一行(不是開(kāi)頭和結(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();
}
}
}
}
//測(cè)試
public static void main(String[] args) {
File file = new File("F:\\myweb");
countCodeLine(file);
}
}
本文標(biāo)題:統(tǒng)計(jì)java文件代碼行數(shù) 統(tǒng)計(jì)java文件代碼行數(shù)和列數(shù)
網(wǎng)頁(yè)鏈接:http://sd-ha.com/article18/doijhdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、建站公司、商城網(wǎng)站、響應(yīng)式網(wǎng)站、App設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)