隨筆:20 文章:1 評論:8 引用:0
╰⊙д⊙╯。oо○
面朝大海·春暖花開
BlogJava
首頁
發新隨筆
發新文章
聯系
聚合
管理
計算指定路徑下所有java文件的代碼行數
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
+=
"
**
"
;
}
//
當且僅當此抽象路徑名表示的文件存在且是一個目錄時,返回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
)
{
//
總行數
tatolLines
++
;
//
計算空行
whiteLines(lineStr);
//
統計代碼行數
commendLines(lineStr);
//
計算代碼的行數
//
codeLines(lineStr);
}
}
catch
(FileNotFoundException e)
{
System.out.println(
"
文件沒有找到
"
);
}
catch
(IOException ee)
{
System.out.println(
"
輸入輸出異常
"
);
}
finally
{
if
(bf
!=
null
)
{
try
{
bf.close();
bf
=
null
;
}
catch
(Exception e)
{
System.out.println(
"
關閉BufferReader時出錯
"
);
}
}
}
}
//
判斷是否為空行
public
static
void
whiteLines(String lineStr)
{
if
(lineStr.matches(
"
^[\\s&&[^\\n]]*$
"
))
{
whiteLines
++
;
}
}
//
判斷是否是一個注釋行
public
static
void
commendLines(String lineStr)
{
//
這里是單行注釋的如 /*
.. */或/**
. */
if
(lineStr.matches(
"
\\s*/\\*{1,}.*(\\*/).*
"
))
{
commentLines
++
;
}
//
這里是多行注釋的
//
這里的是當開始為/**或/*但是沒有 */ 關閉時
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
++
;
}
}
}
發表于 2008-04-29 22:21
dreamingnest
閱讀(861)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
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
常用鏈接
我的隨筆
我的文章
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
(13)
應用程序(4)
(rss)
數據結構(java)
(rss)
算法程序總結(2)
(rss)
鏈表和棧(結)(7)
(rss)
隨筆檔案
(21)
2008年10月 (1)
2008年5月 (7)
2008年4月 (13)
外面的世界
懶散狂徒的專欄(天行健,君子以自強不息 地勢坤,君子以厚德載物)
(rss)
這里的朋友
保爾任(思想比知識更重要 成長比成功更重要)
搜索
最新評論
1.?re: BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件
學習了
--fejay
2.?re: 關于螞蟻問題(Ants)
實際過程可以這么進行抽象模擬:
序列中的元素帶有方向,進行負值部分移動到負值區域,正值部分移動到正值區域時就不再發生碰撞,此時絕對值最小的值決定剩余爬行時間
--zdh
3.?re: 關于螞蟻問題(Ants)
這個問題看到實質就很簡單,所有的螞蟻都是相同的螞蟻,因此可以看成所有的螞蟻都可以穿過對面爬過來的螞蟻就ok啦,最長時間就是兩端的螞蟻向另一端爬出去,最短的就是兩端的四個螞蟻向所在端爬出:)
--zdh
4.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--blues
5.?re: 關于螞蟻問題(Ants)
評論內容較長,點擊標題查看
--dreamingnest
閱讀排行榜
1.?關于螞蟻問題(Ants)(2251)
2.?通過排序總結java泛型數組列表(1652)
3.?堆棧解(非遞歸)決迷宮問題(1417)
4.?ACM中使用JAVA的介紹(1049)
5.?~·掃雷小游戲·~(1039)
評論排行榜
1.?關于螞蟻問題(Ants)(7)
2.?BFS和DFS兩種方法獲取指定目錄下的所有目錄和文件(1)
3.?一著名軟件公司的java筆試算法題的答案 (0)
4.?堆棧解(非遞歸)決迷宮問題(0)
5.?堆排序代碼(0)
Powered By:
博客園
模板提供
:
滬江博客
主站蜘蛛池模板:
紫金县
|
苍溪县
|
分宜县
|
柏乡县
|
安化县
|
垦利县
|
泰和县
|
清丰县
|
荔浦县
|
莲花县
|
疏附县
|
长武县
|
宜川县
|
菏泽市
|
贵德县
|
和田县
|
凉山
|
二手房
|
华亭县
|
孟连
|
大冶市
|
灌云县
|
邻水
|
钟祥市
|
瓦房店市
|
三门县
|
嘉黎县
|
福泉市
|
昌都县
|
浏阳市
|
界首市
|
滨海县
|
凌云县
|
那坡县
|
娱乐
|
新疆
|
赤壁市
|
宣武区
|
佛学
|
普洱
|
陕西省
|