java wordcount程序_WordCount程序(java)
一、題目描述
實(shí)現(xiàn)一個(gè)簡(jiǎn)單而完整的軟件工具(源程序特征統(tǒng)計(jì)程序)。
進(jìn)行單元測(cè)試、回歸測(cè)試、效能測(cè)試,在實(shí)現(xiàn)上述程序的過(guò)程中使用相關(guān)的工具。
進(jìn)行個(gè)人軟件過(guò)程(PSP)的實(shí)踐,逐步記錄自己在每個(gè)軟件工程環(huán)節(jié)花費(fèi)的時(shí)間。
二、WC 項(xiàng)目要求
wc.exe 是一個(gè)常見(jiàn)的工具,它能統(tǒng)計(jì)文本文件的字符數(shù)、單詞數(shù)和行數(shù)。這個(gè)項(xiàng)目要求寫一個(gè)命令行程序,模仿已有wc.exe 的功能,并加以擴(kuò)充,給出某程序設(shè)計(jì)語(yǔ)言源文件的字符數(shù)、單詞數(shù)和行數(shù)。
實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)程序,它能正確統(tǒng)計(jì)程序文件中的字符數(shù)、單詞數(shù)、行數(shù),以及還具備其他擴(kuò)展功能,并能夠快速地處理多個(gè)文件。
具體功能要求:程序處理用戶需求的模式為: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("系統(tǒng)找不到指定路徑文件");
}returnbufferedReader;
}
返回文件字符數(shù)
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;
}
返回文件行數(shù)
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;
}
返回文件單詞數(shù)
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;//判斷是否處于多行注解內(nèi)
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("該目錄下無(wú)對(duì)應(yīng)文件");
}
}else{
System.out.println("該目錄為空");
}
}
}else{
System.out.println("參數(shù)不是文件夾或路徑錯(cuò)誤");
}
}/***
* 遞歸子文件
*@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+"】文件的字符數(shù):" +CharCount(f.getPath()));break;case "-w":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的單詞數(shù):" +WordCount(f.getPath()));break;case "-l":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的行數(shù):" +LineCount(f.getPath()));break;case "-a":
System.out.println("【"+parentName+"】下的【"+fName+"】文件的信息:");
Complex(f.getPath());break;default:
System.out.println("第二參數(shù)錯(cuò)誤(-c,-w,-l)");return false;
}
flag= true;
}
}else{ //子文件是目錄
childFlag = ReadFold(operation, f.getName(), f.listFiles(), frontName, extension) ||childFlag;}
}return (flag ||childFlag);
}
主函數(shù)調(diào)用
/*** 正確傳參*/
private static int FLAG_TWO = 2;/*** 出錯(cuò)誤返回值*/
private static int ERROR_NUM = -1;public static voidmain(String[] args) {if(args.length >=FLAG_TWO){switch (args[0]){//字符數(shù)
case "-c":long startC =System.currentTimeMillis();
System.out.println("字符數(shù):"+CharCount(args[1]));
System.out.printf("耗時(shí)%d(毫秒)\n", System.currentTimeMillis()-startC);break;//單詞數(shù)
case "-w":long startW =System.currentTimeMillis();
System.out.println("單詞數(shù):"+WordCount(args[1]));
System.out.printf("耗時(shí)%d(毫秒)\n", System.currentTimeMillis()-startW);break;//行數(shù)
case "-l":long startL =System.currentTimeMillis();
System.out.println("行數(shù):"+LineCount(args[1]));
System.out.printf("耗時(shí)%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("耗時(shí)%d(毫秒)\n", System.currentTimeMillis()-startS);
}else{
System.out.println("正確格式為:wc.exe -s 【操作】 文件夾路徑 文件名");
}break;//返回更復(fù)雜的數(shù)據(jù)(代碼行 / 空行 / 注釋行)。
case "-a":long startA =System.currentTimeMillis();
Complex(args[1]);
System.out.printf("耗時(shí)%d(毫秒)\n", System.currentTimeMillis()-startA);break;default:
System.out.println("請(qǐng)輸入正確參數(shù)【操作】+文件路徑");
}
}else{
System.out.println("請(qǐng)輸入正確參數(shù)【操作】+文件路徑");
}
}
四、項(xiàng)目測(cè)試
用exe4j將jar包轉(zhuǎn)exe程序再測(cè)試
五、PSP
PSP2.1
Personal Software Process Stages
預(yù)估耗時(shí)(分鐘)
實(shí)際耗時(shí)(分鐘)
Planning
計(jì)劃
10
15
· Estimate
· 估計(jì)這個(gè)任務(wù)需要多少時(shí)間
200
300
Development
開(kāi)發(fā)
120
200
· Analysis
· 需求分析 (包括學(xué)習(xí)新技術(shù))
15
10
· Design Spec
· 生成設(shè)計(jì)文檔
10
10
· Design Review
· 設(shè)計(jì)復(fù)審 (和同事審核設(shè)計(jì)文檔)
10
10
· Coding Standard
· 代碼規(guī)范 (為目前的開(kāi)發(fā)制定合適的規(guī)范)
5
5
· Design
· 具體設(shè)計(jì)
30
25
· Coding
· 具體編碼
100
200
· Code Review
· 代碼復(fù)審
10
15
· Test
· 測(cè)試(自我測(cè)試,修改代碼,提交修改)
10
30
Reporting
報(bào)告
15
20
· Test Report
· 測(cè)試報(bào)告
10
30
· Size Measurement
· 計(jì)算工作量
15
20
· Postmortem & Process Improvement Plan
· 事后總結(jié), 并提出過(guò)程改進(jìn)計(jì)劃
15
30
合計(jì)
575
920
六、總結(jié)
實(shí)際時(shí)間總比預(yù)期長(zhǎng),敲代碼容易改代碼難,思路要清晰,讀取文件用緩沖流效果更好,將jar包轉(zhuǎn)exe再執(zhí)行程序感覺(jué)比直接運(yùn)行慢,” || “判斷只要前者為真后者就不再執(zhí)行!!!學(xué)會(huì)了簡(jiǎn)單使用git管理項(xiàng)目。
總結(jié)
以上是生活随笔為你收集整理的java wordcount程序_WordCount程序(java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 双栈排序java_双栈排序(Java)
- 下一篇: 中央处理器属于计算机外部设备吗,不属于计