隨筆:20 文章:1 評(píng)論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開(kāi)
BlogJava
首頁(yè)
發(fā)新隨筆
發(fā)新文章
聯(lián)系
聚合
管理
計(jì)算指定路徑下所有java文件的代碼行數(shù)
import
java.io.File;
import
java.io.FileNotFoundException;
import
java.io.BufferedReader;
import
java.io.FileReader;
import
java.io.IOException;
public
class
CountCodeLines
{
private
static
int
codeLines
=
0
;
private
static
int
whiteLines
=
0
;
private
static
int
commentLines
=
0
;
private
static
int
tatolLines
=
0
;
private
static
boolean
bComment
=
false
;
public
static
void
main(String[] args)
{
StringBuffer pathName
=
new
StringBuffer(
"
c:/test/
"
);
ComputeDirectoryAndFiles(pathName,
0
);
codeLines
=
tatolLines
-
commentLines
-
whiteLines;
System.out.println(
"
Code Lines:
"
+
codeLines
+
"
\n
"
+
"
White Lines:
"
+
whiteLines
+
"
\n
"
+
"
Comment Lines:
"
+
commentLines);
}
public
static
void
ComputeDirectoryAndFiles(StringBuffer pathName,
int
level)
{
File directory
=
new
File(pathName.toString());
File[] files
=
directory.listFiles();
String prefix
=
""
;
for
(
int
i
=
0
; i
<
files.length; i
++
)
{
prefix
+=
"
**
"
;
}
//
當(dāng)且僅當(dāng)此抽象路徑名表示的文件存在且是一個(gè)目錄時(shí),返回true;否則返回 false
if
(directory.isDirectory())
{
for
(
int
i
=
0
; i
<
files.length; i
++
)
{
if
(files[i].isFile()
&&
files[i].getName().matches(
"
^[a-zA-Z[^0-9]]\\w*.java$
"
))
{
computeLines(files[i]);
}
if
(files[i].isDirectory())
{
pathName.append(
"
/
"
+
files[i].getName());
level
++
;
ComputeDirectoryAndFiles(pathName, level);
int
start
=
pathName.toString().length()
-
files[i].getName().length()
-
1
;
int
end
=
pathName.toString().length();
pathName.delete(start, end);
level
--
;
}
}
}
}
public
static
void
computeLines(File file)
{
BufferedReader bf
=
null
;
try
{
bf
=
new
BufferedReader(
new
FileReader(file));
String lineStr
=
""
;
while
((lineStr
=
bf.readLine())
!=
null
)
{
//
總行數(shù)
tatolLines
++
;
//
計(jì)算空行
whiteLines(lineStr);
//
統(tǒng)計(jì)代碼行數(shù)
commendLines(lineStr);
//
計(jì)算代碼的行數(shù)
//
codeLines(lineStr);
}
}
catch
(FileNotFoundException e)
{
System.out.println(
"
文件沒(méi)有找到
"
);
}
catch
(IOException ee)
{
System.out.println(
"
輸入輸出異常
"
);
}
finally
{
if
(bf
!=
null
)
{
try
{
bf.close();
bf
=
null
;
}
catch
(Exception e)
{
System.out.println(
"
關(guān)閉BufferReader時(shí)出錯(cuò)
"
);
}
}
}
}
//
判斷是否為空行
public
static
void
whiteLines(String lineStr)
{
if
(lineStr.matches(
"
^[\\s&&[^\\n]]*$
"
))
{
whiteLines
++
;
}
}
//
判斷是否是一個(gè)注釋行
public
static
void
commendLines(String lineStr)
{
//
這里是單行注釋的如 /*
.. */或/**
. */
if
(lineStr.matches(
"
\\s*/\\*{1,}.*(\\*/).*
"
))
{
commentLines
++
;
}
//
這里是多行注釋的
//
這里的是當(dāng)開(kāi)始為/**或/*但是沒(méi)有 */ 關(guān)閉時(shí)
else
if
(lineStr.matches(
"
\\s*/\\*{1,}.*[^\\*/].*
"
))
{
commentLines
++
;
bComment
=
true
;
}
else
if
(
true
==
bComment)
{
commentLines
++
;
if
(lineStr.matches(
"
\\s*[\\*/]+\\s*
"
))
{
bComment
=
false
;
}
}
else
if
(lineStr.matches(
"
^[\\s]*//.*
"
))
{
commentLines
++
;
}
}
}
發(fā)表于 2008-04-29 22:21
dreamingnest
閱讀(861)
評(píng)論(0)
編輯
收藏
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問(wèn)
管理
CALENDER
<
2008年4月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
常用鏈接
我的隨筆
我的文章
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(1)
給我留言
查看公開(kāi)留言
查看私人留言
隨筆分類
(13)
應(yīng)用程序(4)
(rss)
數(shù)據(jù)結(jié)構(gòu)(java)
(rss)
算法程序總結(jié)(2)
(rss)
鏈表和棧(結(jié))(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強(qiáng)不息 地勢(shì)坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識(shí)更重要 成長(zhǎng)比成功更重要)
搜索
最新評(píng)論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學(xué)習(xí)了
--fejay
2.?re: 關(guān)于螞蟻問(wèn)題(Ants)
實(shí)際過(guò)程可以這么進(jìn)行抽象模擬:
序列中的元素帶有方向,進(jìn)行負(fù)值部分移動(dòng)到負(fù)值區(qū)域,正值部分移動(dòng)到正值區(qū)域時(shí)就不再發(fā)生碰撞,此時(shí)絕對(duì)值最小的值決定剩余爬行時(shí)間
--zdh
3.?re: 關(guān)于螞蟻問(wèn)題(Ants)
這個(gè)問(wèn)題看到實(shí)質(zhì)就很簡(jiǎn)單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過(guò)對(duì)面爬過(guò)來(lái)的螞蟻就ok啦,最長(zhǎng)時(shí)間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個(gè)螞蟻向所在端爬出:)
--zdh
4.?re: 關(guān)于螞蟻問(wèn)題(Ants)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--blues
5.?re: 關(guān)于螞蟻問(wèn)題(Ants)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--dreamingnest
閱讀排行榜
1.?關(guān)于螞蟻問(wèn)題(Ants)(2251)
2.?通過(guò)排序總結(jié)java泛型數(shù)組列表(1652)
3.?堆棧解(非遞歸)決迷宮問(wèn)題(1417)
4.?ACM中使用JAVA的介紹(1049)
5.?~·掃雷小游戲·~(1039)
評(píng)論排行榜
1.?關(guān)于螞蟻問(wèn)題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問(wèn)題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
衢州市
|
景东
|
南丹县
|
昌图县
|
长丰县
|
沽源县
|
高碑店市
|
和平区
|
新泰市
|
林芝县
|
兴国县
|
百色市
|
迁安市
|
宝应县
|
沁阳市
|
崇信县
|
高尔夫
|
长海县
|
满洲里市
|
江孜县
|
双江
|
伊宁县
|
蕉岭县
|
桃源县
|
东兴市
|
海安县
|
闽侯县
|
阿巴嘎旗
|
瓮安县
|
钟山县
|
襄汾县
|
陆丰市
|
阿拉尔市
|
龙里县
|
桃园市
|
山东省
|
鹤山市
|
肃宁县
|
许昌县
|
田林县
|
罗江县
|