java wordcount程序_WordCount程序(java)
一、題目描述
實現一個簡單而完整的軟件工具(源程序特征統計程序)。
進行單元測試、回歸測試、效能測試,在實現上述程序的過程中使用相關的工具。
進行個人軟件過程(PSP)的實踐,逐步記錄自己在每個軟件工程環節花費的時間。
二、WC 項目要求
wc.exe 是一個常見的工具,它能統計文本文件的字符數、單詞數和行數。這個項目要求寫一個命令行程序,模仿已有wc.exe 的功能,并加以擴充,給出某程序設計語言源文件的字符數、單詞數和行數。
實現一個統計程序,它能正確統計程序文件中的字符數、單詞數、行數,以及還具備其他擴展功能,并能夠快速地處理多個文件。
具體功能要求:程序處理用戶需求的模式為:wc.exe [parameter] [file_name]
三、核心代碼
獲取文件字符緩存流
private staticBufferedReader GetFileInmputStream(String fileName){
BufferedReader bufferedReader= null;try{
bufferedReader= new BufferedReader(new InputStreamReader(newFileInputStream(fileName)));
}catch(FileNotFoundException e) {
System.out.println("系統找不到指定路徑文件");
}returnbufferedReader;
}
返回文件字符數
public static intCharCount(String fileName){
BufferedReader in= null;int count = 0;
in=GetFileInmputStream(fileName);if (in == null){returnERROR_NUM;
}int result =ERROR_NUM;try{while((result = in.read()) != -1){if(result != '\r' && result!='\n'){
count++;
}
}
}catch(IOException e) {
e.printStackTrace();
}finally{if (in != null){try{
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returncount;
}
返回文件行數
public static intLineCount(String fileName){
BufferedReader read= null;int count = 0;
read=GetFileInmputStream(fileName);if (read == null){returnERROR_NUM;
}try{while (read.readLine() != null){
count++;
}//速度較前者慢//Iterator iterator = read.lines().iterator();//while (iterator.hasNext()){//iterator.next();//count++;//}
}catch(IOException e) {
e.printStackTrace();
}finally{if(read != null){try{
read.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returncount;
}
返回文件單詞數
public static intWordCount(String fileName){
BufferedReader in= null;int count = 0;
in=GetFileInmputStream(fileName);if (in == null){returnERROR_NUM;
}try{
String str= null;while((str = in.readLine()) != null){//\\s+表示 空格,回車,換行等空白符
String[] split = str.split("\\s+");
count+=split.length;
}
}catch(IOException e) {
e.printStackTrace();
}finally{if (in != null){try{
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returncount;
}
顯示(代碼行 / 空行 / 注釋行)
public static voidComplex(String filename){int codeLine = 0;int annotationLine = 0;int blankLine = 0;//判斷是否處于多行注解內
boolean flag = false;
BufferedReader bufferedReader=GetFileInmputStream(filename);if (bufferedReader == null){return;
}
String strLine= null;
String newLine= null;try{while ((strLine = bufferedReader.readLine())!= null){if(flag) {if (strLine.endsWith("*/")){
flag= false;
}
annotationLine++;
}else{//去除空格
newLine = strLine.replaceAll("\\s*", "");if("".equals(newLine)){//空行
blankLine++;
}else if (newLine.startsWith("/*")){//注釋行
if (!newLine.endsWith("*/")){//去除/*單行注釋*/情況
flag = true;
}
annotationLine++;
}else if (newLine.startsWith("//") || newLine.startsWith("}//")){//注釋行
annotationLine++;
}else{//代碼行
codeLine++;
}
}
}
System.out.println("代碼行:"+codeLine);
System.out.println("空行:"+blankLine);
System.out.println("注釋行:"+annotationLine);
}catch(IOException e) {
e.printStackTrace();
}finally{if (bufferedReader != null){try{
bufferedReader.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
遞歸處理目錄
public static voidFolderCount(String operation, String folderName, String fileName){
File file= newFile(folderName);if(file.isDirectory()){int index = fileName.lastIndexOf('.');if(index >= 0){
String extension=fileName.substring(index);
String frontName= fileName.substring(0, index);
File[] fileList=file.listFiles();if (fileList.length > 0){//String regex = ".*"+name+"\\.+"+extension+"$";//for (String f:fileList){//System.out.println(Pattern.matches(regex, f));//System.out.println(f);//}//遞歸遍歷
if (!ReadFold(operation, file.getName(), fileList, frontName, extension)){
System.out.println("該目錄下無對應文件");
}
}else{
System.out.println("該目錄為空");
}
}
}else{
System.out.println("參數不是文件夾或路徑錯誤");
}
}/***
* 遞歸子文件
*@paramoperation
*@paramfileList
*@paramfrontName
*@paramextension*/
public static booleanReadFold(String operation, String parentName, File[] fileList, String frontName, String extension){boolean flag = false;boolean childFlag = false;
String fName= null;for(File f:fileList){//子文件是文件
if(f.isFile()){
fName=f.getName();if (fName.indexOf(frontName) >=0 &&fName.endsWith(extension)) {switch(operation) {case "-c":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的字符數:" +CharCount(f.getPath()));break;case "-w":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的單詞數:" +WordCount(f.getPath()));break;case "-l":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的行數:" +LineCount(f.getPath()));break;case "-a":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的信息:");
Complex(f.getPath());break;default:
System.out.println("第二參數錯誤(-c,-w,-l)");return false;
}
flag= true;
}
}else{ //子文件是目錄
childFlag = ReadFold(operation, f.getName(), f.listFiles(), frontName, extension) ||childFlag;}
}return (flag ||childFlag);
}
主函數調用
/*** 正確傳參*/
private static int FLAG_TWO = 2;/*** 出錯誤返回值*/
private static int ERROR_NUM = -1;public static voidmain(String[] args) {if(args.length >=FLAG_TWO){switch (args[0]){//字符數
case "-c":long startC =System.currentTimeMillis();
System.out.println("字符數:"+CharCount(args[1]));
System.out.printf("耗時%d(毫秒)\n", System.currentTimeMillis()-startC);break;//單詞數
case "-w":long startW =System.currentTimeMillis();
System.out.println("單詞數:"+WordCount(args[1]));
System.out.printf("耗時%d(毫秒)\n", System.currentTimeMillis()-startW);break;//行數
case "-l":long startL =System.currentTimeMillis();
System.out.println("行數:"+LineCount(args[1]));
System.out.printf("耗時%d(毫秒)\n", System.currentTimeMillis()-startL);break;//遞歸處理目錄下符合條件的文件。
case "-s":if(args.length == 4){long startS =System.currentTimeMillis();
FolderCount(args[1], args[2], args[3]);
System.out.printf("耗時%d(毫秒)\n", System.currentTimeMillis()-startS);
}else{
System.out.println("正確格式為:wc.exe -s 【操作】 文件夾路徑 文件名");
}break;//返回更復雜的數據(代碼行 / 空行 / 注釋行)。
case "-a":long startA =System.currentTimeMillis();
Complex(args[1]);
System.out.printf("耗時%d(毫秒)\n", System.currentTimeMillis()-startA);break;default:
System.out.println("請輸入正確參數【操作】+文件路徑");
}
}else{
System.out.println("請輸入正確參數【操作】+文件路徑");
}
}
四、項目測試
用exe4j將jar包轉exe程序再測試
五、PSP
PSP2.1
Personal Software Process Stages
預估耗時(分鐘)
實際耗時(分鐘)
Planning
計劃
10
15
· Estimate
· 估計這個任務需要多少時間
200
300
Development
開發
120
200
· Analysis
· 需求分析 (包括學習新技術)
15
10
· Design Spec
· 生成設計文檔
10
10
· Design Review
· 設計復審 (和同事審核設計文檔)
10
10
· Coding Standard
· 代碼規范 (為目前的開發制定合適的規范)
5
5
· Design
· 具體設計
30
25
· Coding
· 具體編碼
100
200
· Code Review
· 代碼復審
10
15
· Test
· 測試(自我測試,修改代碼,提交修改)
10
30
Reporting
報告
15
20
· Test Report
· 測試報告
10
30
· Size Measurement
· 計算工作量
15
20
· Postmortem & Process Improvement Plan
· 事后總結, 并提出過程改進計劃
15
30
合計
575
920
六、總結
實際時間總比預期長,敲代碼容易改代碼難,思路要清晰,讀取文件用緩沖流效果更好,將jar包轉exe再執行程序感覺比直接運行慢,” || “判斷只要前者為真后者就不再執行!!!學會了簡單使用git管理項目。
總結
以上是生活随笔為你收集整理的java wordcount程序_WordCount程序(java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双栈排序java_双栈排序(Java)
- 下一篇: matlab compiler 与mat