今天終於來臨了,今天可算到來了,今天已經實現了!三年的辛苦與汗水,無數的歡歌與笑語.2010年4月6日,是我值得紀念的一天,因爲今天我突破100000行代碼了,我等了好久了,2010年最初磕磕碰碰的我,現在有回到從前了,並且更加自信!2010年目標320000行代碼,李開復說過微軟應聘大學生要求100000行代碼,我基本滿足了,我用我自己編的代碼統計小工具測得我的代碼量。100%正確率,因爲它用到了正則表達式,文件等一些概念,要說出錯,那只有電腦出錯的那億萬分之一得機率了。現在有彙編語言寫過的100多行程序,C語言寫過的3000行左右程序,C++寫過的20000行左右的程序,Java寫過的60000行左右的程序,還有現在的HTML,JSP,JavaScript, J2ME, Android寫過的10000行左右的程序,再加上數據結構,編譯原理,VC++等10000行左右的程序。總數>100000行,代碼數已經是六位數了,但我知道今天只是個開始,今天只是個起步,總有那麼一天我會自豪的說,我寫過的程序就像冬天的雪花那樣,讓人眼紅繚亂,讓人數不勝數。
import java.util.regex.*;
import java.io.File;
import
java.io.FileReader;
import java.io.FileNotFoundException;
import
java.io.IOException;
import java.io.BufferedReader;
import
java.awt.*;
import java.awt.event.*;
public class CodeCount
{
//定義統計空行、正常、註釋的變量
static long
whitelines = 0;
static long normallines = 0;
static long commentlines
= 0;
Frame frame = new Frame("代碼統計小工具");
FileDialog open = new
FileDialog(frame, "選擇需要打開的文件夾", FileDialog.LOAD);
Button bt = new
Button("打開要統計的文件夾中的一個文件");
TextArea ta = new TextArea(5,
30);
//存放要統計文件夾得路徑
String str = null;
public void
init()
{
ta.setBackground(new Color(255, 134, 100));
ta.setEditable(false);
bt.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent
e)
{
open.setVisible(true);
str =
open.getDirectory();
File f = new File(str);
File[] codeFiles = f.listFiles();
for(File child :
codeFiles)
{
//只對後綴名爲java的文件進行分析
if(child.getName().matches(".*//.java$"))
{
parse(child);
}
}
String ss = "空行個數:
" + whitelines +
"/n正常代碼行個數:
" + normallines +
"/n註釋行個數: " +
commentlines;
ta.setText(ss);
}
});
frame.addWindowListener(new
WindowAdapter()
{
public void
windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.setBounds(20, 30, 200, 300);
frame.add(ta,
BorderLayout.EAST);
frame.add(bt, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
public void
parse(File f)
{
BufferedReader br = null;
boolean comment =
false;
try
{
//BuffeedReader
構造函數用的是Reader類型的,所以用FileReader
br = new BufferedReader(new
FileReader(f));
String line = "";
while((line =
br.readLine())!=null)
{
line =
line.trim();
//由於readline已經把結尾的換行符給抹掉了所以求空白符的正則表達式不能以換行符結束了
if(line.matches("^[//s&&[^//n]]*$"))
{
whitelines++;
}
else if(line.startsWith("/*")
&& !line.endsWith("*/"))
{
commentlines++;
comment = true;
}
else if(line.startsWith("/*") &&
line.endsWith("*/"))
{
commentlines++;
}
else if(true == comment)
{
commentlines++;
if(line.endsWith("*/") ||
line.startsWith("*/"))
comment = false;
}
else if(line.startsWith("//"))
{
commentlines++;
}
else
{
normallines++;
}
}
}
catch(FileNotFoundException
e)
{
e.printStackTrace();
}
catch(IOException c)
{
c.printStackTrace();
}
finally
{
if(br != null)
{
try
{
br.close();
br = null;
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
public static void main(String[]
args)
{
CodeCount cc = new CodeCount();
cc.init();
}
}
我,安樹峯,對自己的前途負責,寫下今天的收穫,告訴世界我在進步!The road ahead will be long. However, I want to go ahead. Today I have to make a choice. I must forget somthing and I need to follow my heart!
:2010年我命運的轉折點兒。