資源簡介
西工大 軟件工程 project 代碼統(tǒng)計 實現(xiàn) MFC和java版本都有

代碼片段和文件信息
/**
?*?讀取文本文件
?*?JavaAlpha
?*/
package?counter;
import?java.io.*;
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.JOptionPane;
public?class?jtxtfm?{
public?static?void?main(String?args[])?{
jtxtfrm?fm?=?new?jtxtfrm();?//這里是程序開始執(zhí)行的地方,新建一個窗口
}
}
class?jtxtfrm?extends?frame?implements?ActionListener?{
FileDialog op?sv;//文件對話框
Button btn1?btn2?btn3;//按鈕
TextArea tarea;//文本框
jtxtfrm()?{
super(“統(tǒng)計代碼工具“);//標(biāo)題
setLayout(null);//布局
setBackground(Color.white);//背景色
setSize(500?300);//設(shè)置大小
setVisible(true);//設(shè)置可見
btn1?=?new?Button(“統(tǒng)計“);//創(chuàng)建按鈕
btn2?=?new?Button(“保存“);
btn3?=?new?Button(“關(guān)閉“);
tarea?=?new?TextArea(““);//創(chuàng)建文本框
add(btn1);//添加按鈕
add(btn2);
add(btn3);
add(tarea);
tarea.setBounds(30?50?350?200);//設(shè)置文本框位置
btn1.setBounds(420?60?50?30);
btn2.setBounds(420?120?50?30);
btn3.setBounds(420?180?50?30);
op?=?new?FileDialog(this?“打開“?FileDialog.LOAD);//新建對話框
sv?=?new?FileDialog(this?“保存“?FileDialog.SAVE);
btn1.addActionListener(this);//設(shè)置按鈕點擊監(jiān)聽事件
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(new?WindowAdapter()?{//關(guān)閉事件處理
public?void?windowClosing(WindowEvent?e)?{
setVisible(false);//設(shè)置不可見
System.exit(0);//程序關(guān)閉
}
});
}
public?void?actionPerformed(ActionEvent?e)?{
?long?normalLines?=?0;
?long?totalLines=0;
?long?commentLines?=?0;
long?whiteLines?=?0;
boolean?comment?=?false;
if?(e.getSource()?==?btn1)?{//按鈕1事件處理
op.setVisible(true);//設(shè)置可見
try?{
File?f1?=?new?File(op.getDirectory()?op.getFile());//打開文件
FileReader?fr?=?new?FileReader(f1);
BufferedReader?br?=?new?BufferedReader(fr);//讀取文件
tarea.setText(““);//設(shè)置文本框內(nèi)容為空
String?str=““;
while?((str?=?br.readLine())?!=?null)
{
totalLines++;
str?=?str.trim();
if(str.matches(“^[\\s&&[^\\n]]*$“))?{
whiteLines?++;
}?else?if?(str.startsWith(“/*“)?&&?!str.endsWith(“*/“))?{
commentLines?++;
comment?=?true;
}?else?if?(str.startsWith(“/*“)?&&?str.endsWith(“*/“))?{
commentLines?++;
}?else?if?(true?==?comment)?{
commentLines?++;
if(str.endsWith(“*/“))?{
comment?=?false;
}
}?else?if?(str.startsWith(“//“))?{
commentLines?++;
}?else?{
normalLines?++;
}
}
tarea.setFont(new?Font(“黑體“Font.BOLD32));
tarea.append(“空白行數(shù)“+whiteLines?+?‘\n‘+“注釋行數(shù)“+commentLines+‘\n‘+“代碼有效行數(shù)“+normalLines+‘\n‘+“代碼總行數(shù)“+totalLines);//文本框添加讀取的文件內(nèi)容
fr.close();//關(guān)閉讀取
}?catch?(Exception?e1)?{//如果有錯誤,這里進(jìn)行處理
JOptionPane.showMessageDialog(null?“請輸入統(tǒng)計文件!!!“?“錯誤提示“?JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null?“文字“?“標(biāo)題“?JOptionPane.QUESTION_MESSAGE);
e1.printStackTrace();//打印錯誤信息
}
}
if?(e.getSource()?==?btn2)?{//按鈕1事件處理
sv.setVisible(true);
try?{
File?f1?=?new?File(sv.getDirectory()?sv.getFile());
FileWriter?fw?=?new?FileWriter(f1);
BufferedWriter?bw?=?new?BufferedWriter(fw);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-06-23?01:29??software?enginnering??demo\
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\
?????文件????????4469??2013-06-22?02:20??software?enginnering??demo\CodeCounter.jar
?????文件?????????301??2013-06-22?00:02??software?enginnering??demo\CodeCount\.classpath
?????文件?????????385??2013-06-22?00:02??software?enginnering??demo\CodeCount\.project
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\.settings\
?????文件?????????629??2013-06-22?00:02??software?enginnering??demo\CodeCount\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\bin\
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\bin\counter\
?????文件?????????450??2013-06-22?01:46??software?enginnering??demo\CodeCount\bin\counter\jtxtfm.class
?????文件?????????684??2013-06-22?01:46??software?enginnering??demo\CodeCount\bin\counter\jtxtfrm$1.class
?????文件????????4401??2013-06-22?01:46??software?enginnering??demo\CodeCount\bin\counter\jtxtfrm.class
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\src\
?????目錄???????????0??2013-06-22?02:27??software?enginnering??demo\CodeCount\src\counter\
?????文件????????3557??2013-06-22?01:46??software?enginnering??demo\CodeCount\src\counter\jtxtfm.java
?????文件??????????43??2013-06-22?02:16??software?enginnering??demo\run.bat
評論
共有 條評論