WordCount合作--自己部分

 

 

前言:

(1)合作者:201631062127,201631062625

(2)合作代码地址:WordCount

一.结对的PSP表格:

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

60

55

· Estimate

· 估计这个任务需要多少时间

1200

900

Development

开发

110

134

· Analysis

· 需求分析 (包括学习新技术)

60

80

· Design Spec

· 生成设计文档

50

70

· Design Review

· 设计复审 (和同事审核设计文档)

60

100

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

60

50

· Design

· 具体设计

100

130

· Coding

· 具体编码

90

120

· Code Review

· 代码复审

30

50

· Test

· 测试(自我测试,修改代码,提交修改)

60

50

Reporting

报告

40

60

· Test Report

· 测试报告

30

30

· Size Measurement

· 计算工作量

10

10

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

25

25

 

合计

785

934

 

二.代码互审情况:

  相关情况: 相互之间对代码通看了一边,对一些好的全部借鉴下来,不好的全部丢弃。比如整个的程序流程

我们重新构思了一下。

三.设计过程:

  由于我们是用java语言来编写的,当然首先是面向对象吧,我们先分析这个项目中所存在的对象,进而分析其需要包含的变量函数。

当然了,博客上已经明确说明此次项目需求,只需要依次实现相关方法即可。

  流程图:

  

 

  部分运行截图:

 

 

 

 命令行运行结果:

目标文件:

停用词表:

结果文件:

 

 通配符递归测试:

结果:

 

四.代码说明:

  这次代码和前次基础功能差别不是很大,类就多了一个,总的5个类。

  

  说明:

    Main-----------主函数所在类

    Client----------界面类

    Parameter-----------参数存放的类,所有解析的参数命令放入此类

    Resulte--------------结果集放置类,所有的结果集中放置

    WordCount-------------主要函数所在类(计词,行等)

    Tools-------------------工具类,包含需要用到的函数,具有较高的复用性(参数解析,文件的写入与读取)

 

  另外对以前的WordCount进行了重构,源码如下(包含部分注释):

 

Mian:

 1 import java.io.IOException;
 2 
 3 
 4 public class Main {
 5     //-c   字符数
 6     // -w  词数
 7     // -l  行数
 8     // -o  指定输出文件
 9     // -e  停用词表,接文件名
10     // -a  返回更复杂的数据(代码行 / 空行 / 注释行)
11     // -s  递归处理目录下符合条件的文件,接文件名
12     // -x  调用高级功能
13     //public static final String DEFAULTPARAS[] = {"-c","-w","-l","-o","-e","-a","-s","-x"};//8个
14 
15     public static void main(String[] args) throws IOException {
16         //String canshu = "-w -c -l -a /home/kevin/cb/a.txt -e /home/kevin/s.txt";
17         //canshu = "-x";
18         String canshu = "";
19         canshu = Tools.junctParas(args);
20 
21         //System.out.println(canshu);
22         WordCount wordCount = new WordCount(canshu);
23         wordCount.processFile();
24 
25         //String str = " {}"+System.lineSeparator();
26         //System.out.println(str.replaceAll("[\\s\\{\\}]","6"));
27         //String str = Tools.text2String(new File("/home/kevin/a.txt"));
28         //System.out.println(wordCount.getLineCount(str));
29 
30         //System.out.println(Tools.getRealFileNameFromList("/home/kevin/cb/"));
31 
32         //System.out.println("a*".matches("a..*"));
33     }
34 }
View Code

Client:

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5 import java.io.File;
  6 import java.io.IOException;
  7 
  8 public class Client implements ActionListener {
  9 
 10     /*
 11     * UI相关
 12     * */
 13     private  JButton buttonF = null;
 14     private JButton buttonOK = null;
 15     private JFrame frame = null;
 16     private JTextArea jTextArea = null;
 17     private Container conFile = null;
 18     private Container conResult = null;
 19     private  JLabel jLabel = null;
 20     private JTextField textF = null;
 21     private JFileChooser jFileChooser = null;
 22     private JTabbedPane tabbedPane = null;
 23 
 24     /*
 25     * WordCound 调用参数相关
 26     * */
 27     private File file = null;
 28     private Parameter parameter = null;
 29 
 30     public Client(Parameter parameter){
 31 
 32         this.parameter = parameter;
 33         buttonF = new JButton();
 34         buttonOK = new JButton();
 35         frame = new JFrame("WordCount");
 36         jTextArea = new JTextArea();
 37         conFile=new Container();
 38         conResult=new Container();
 39         jLabel = new JLabel();
 40         textF=new JTextField();
 41         tabbedPane = new JTabbedPane();
 42         jFileChooser = new JFileChooser();
 43 
 44     }
 45 
 46     /**
 47      * @Auther: kevin
 48      * @param
 49      * @return: void
 50      * @Descriptions:
 51      * @date: 18-10-17 下午8:45
 52      */
 53     public void createAndShowGUI() {
 54 
 55         // 确保一个漂亮的外观风格
 56         //JFrame.setDefaultLookAndFeelDecorated(true);
 57 
 58         // 创建及设置窗口
 59         //frame.getContentPane().setBackground(Color.red);
 60         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 61 
 62         /*
 63          *获取屏幕的宽,高
 64          *
 65          * */
 66         double hostL = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
 67         double hostW = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
 68         frame.setLocation((int)(hostL/2-100),(int)(hostW/2-250));//根据面板大小设置面板出现位置
 69         frame.setSize(500,200);//设置整个面板大小
 70         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
 71 
 72         /*
 73          *添加面板
 74          *
 75          * */
 76         tabbedPane.addTab("chose your file",conFile);
 77         tabbedPane.addTab("results",conResult);
 78 
 79         /*
 80          *设置结果显示文本框
 81          *
 82          * */
 83         jTextArea.setBounds(0,0,500,200);
 84         jTextArea.setLineWrap(true);
 85         //jTextArea.setText("464648564444444444444416555555555555555555555555555555555555555555");
 86         //add to pane
 87         conResult.add(jTextArea);
 88 
 89         /*
 90          * 设置文件选择框
 91          * */
 92         jLabel.setText("file: ");
 93         jLabel.setSize(40,40);
 94         jLabel.setLocation(30,40);
 95         conFile.add(jLabel);
 96 
 97         //文件路径名
 98         textF.setSize(180,25);
 99         textF.setLocation(70,50);
100         conFile.add(textF);
101 
102         //文件选择框
103         jFileChooser.setCurrentDirectory(new File("").getAbsoluteFile());
104         conFile.add(jFileChooser);
105 
106         /*
107          *设置文件选择按钮和确认按钮
108          *
109          * */
110         //文字
111         buttonF.setText("chose");
112         buttonOK.setText("sure");
113         //位置
114         buttonF.setLocation(270,50);
115         buttonOK.setLocation(340,50);
116         //大小
117         buttonF.setSize(50,25);
118         buttonOK.setSize(50,25);
119         //内边距
120         buttonF.setMargin(new Insets(0,0,0,0));
121         buttonOK.setMargin(new Insets(0,0,0,0));
122         //加入pane
123         conFile.add(buttonF);
124         conFile.add(buttonOK);
125         //添加监听
126         buttonF.addActionListener(this);
127         buttonOK.addActionListener(this);
128 
129         frame.add(tabbedPane);
130         // 显示窗口
131         frame.setVisible(true);
132 
133     }
134 
135     @Override
136     public void actionPerformed(ActionEvent actionEvent) {
137 
138         if(actionEvent.getSource().equals(buttonF)) {
139             jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//只选择文件
140             int status = jFileChooser.showOpenDialog(null);//文件选择状态
141 
142             if(status==JFileChooser.CANCEL_OPTION){
143                 return;
144             }else if(JFileChooser.APPROVE_OPTION == status){
145 
146                 file=jFileChooser.getSelectedFile();//选择到的文件
147                 //System.out.println(file.exists());
148                 textF.setText(file.getAbsolutePath());
149             }
150 
151         }else if(actionEvent.getSource().equals(buttonOK)){
152             if(file==null){
153                 // 消息对话框无返回, 仅做通知作用
154                 JOptionPane.showMessageDialog( frame, "File is not exist !!!", "error", JOptionPane.INFORMATION_MESSAGE );
155             }else{
156 
157                 tabbedPane.setSelectedComponent(conResult);
158                 setParameter(parameter ,file);
159                 Result result = new Result();
160                 String textData = Tools.text2String(new File(this.parameter.getInputfileName()));
161                 try {
162                     Tools.setResult(new WordCount(""),parameter,result,textData);
163                 } catch (IOException e) {
164                     e.printStackTrace();
165                 }
166                 //设置显示数据
167                 String sResult= Tools.formatInputData(parameter.getInputfileName(),result.getCharCount(),result.getWordCount(),result.getLineCount(),result.getCodeLineCount(),result.getCommentLineCount(),result.getEmptyLineCount());
168                 jTextArea.setText(sResult);
169             }
170         }
171     }
172 
173     /**
174      * @Auther: kevin
175      * @param parameter
176     * @param file
177      * @return: void
178      * @Descriptions:
179      * @date: 18-10-17 下午8:44
180      */
181     private void setParameter(Parameter parameter ,File file){
182 
183         parameter.setInputfileName(file.getAbsolutePath());
184         parameter.setShowCharCount(true);
185         parameter.setShowWordCount(true);
186         parameter.setShowlineCount(true);
187         parameter.setShowMore(true);
188     }
189 
190 
191     /*public static void main(String[] args){
192         Client swingUI = new Client();
193         swingUI.createAndShowGUI();
194         int a = 0,b=1;
195         if(a==0){
196             System.out.println("0");
197         }else if(b==1){
198             System.out.println("1");
199         }
200 
201     }*/
202 }
View Code

Parameter:

  1 import java.util.List;
  2 
  3 public class Parameter {
  4     //-c   字符数
  5     // -W  词数
  6     // -l  行数
  7     // -o  指定输出文件
  8     // -e  停用词表,接文件名
  9     // -a  返回更复杂的数据(代码行 / 空行 / 注释行)
 10     // -s  递归处理目录下符合条件的文件,接文件名
 11     // -x  调用高级功能
 12 
 13     //单文件使用的输入名
 14     private String inputfileName=null;
 15 
 16     //默认输出文件
 17     private String outPutFileName="result.txt";
 18 
 19     //停用词表
 20     private String filterFilename = null;
 21 
 22     //多文件使用
 23     private List<String> filesName = null;
 24 
 25     //是否计算字符数
 26     private boolean isShowCharCount=false;
 27 
 28     //是否计算词数
 29     private boolean isShowWordCount=false;
 30 
 31     //是否计算行数
 32     private boolean isShowlineCount=false;
 33 
 34     //是否有指定输出文件
 35     private boolean isOutput=false;
 36 
 37     //是否用停用词表
 38     private boolean filterWord=false;
 39 
 40     //是否显示更多信息(代码行,注释行,空行)
 41     private boolean isShowMore=false;
 42 
 43     //是否处理多文件,就是递归处理符合条件的文件
 44     private boolean isProcessMultFiles=false;
 45 
 46     //是否启用高级功能,UI界面
 47     private boolean isAdvance=false;
 48 
 49 
 50     public String getInputfileName() {
 51         return inputfileName;
 52     }
 53 
 54     public void setInputfileName(String inputfileName) {
 55         this.inputfileName = inputfileName;
 56     }
 57 
 58     public String getOutPutFileName() {
 59         return outPutFileName;
 60     }
 61 
 62     public void setOutPutFileName(String outPutFileName) {
 63         this.outPutFileName = outPutFileName;
 64     }
 65 
 66     public String getFilterFilename() {
 67         return filterFilename;
 68     }
 69 
 70     public void setFilterFilename(String filterFilename) {
 71         this.filterFilename = filterFilename;
 72     }
 73 
 74     public List<String> getFilesName() {
 75         return filesName;
 76     }
 77 
 78     public void setFilesName(List<String> filesName) {
 79         this.filesName = filesName;
 80     }
 81 
 82     public boolean isShowCharCount() {
 83         return isShowCharCount;
 84     }
 85 
 86     public void setShowCharCount(boolean showCharCount) {
 87         isShowCharCount = showCharCount;
 88     }
 89 
 90     public boolean isShowWordCount() {
 91         return isShowWordCount;
 92     }
 93 
 94     public void setShowWordCount(boolean showWordCount) {
 95         isShowWordCount = showWordCount;
 96     }
 97 
 98     public boolean isShowlineCount() {
 99         return isShowlineCount;
100     }
101 
102     public void setShowlineCount(boolean showlineCount) {
103         isShowlineCount = showlineCount;
104     }
105 
106     public boolean isOutput() {
107         return isOutput;
108     }
109 
110     public void setOutput(boolean output) {
111         isOutput = output;
112     }
113 
114     public boolean isFilterWord() {
115         return filterWord;
116     }
117 
118     public void setFilterWord(boolean filterWord) {
119         this.filterWord = filterWord;
120     }
121 
122     public boolean isShowMore() {
123         return isShowMore;
124     }
125 
126     public void setShowMore(boolean showMore) {
127         isShowMore = showMore;
128     }
129 
130     public boolean isProcessMultFiles() {
131         return isProcessMultFiles;
132     }
133 
134     public void setProcessMultFiles(boolean processMultFiles) {
135         isProcessMultFiles = processMultFiles;
136     }
137 
138     public boolean isAdvance() {
139         return isAdvance;
140     }
141 
142     public void setAdvance(boolean advance) {
143         isAdvance = advance;
144     }
145 }
View Code

Result:

 1 public class Result {
 2     //处理的文件名
 3     private String inputFileName=null;
 4     //指定输出的文件名
 5     private String outPutFileName=null;
 6 
 7     //字符数
 8     private int charCount=-1;
 9     //词数
10     private int WordCount=-1;
11     //行数
12     private int lineCount=-1;
13 
14     //空行数
15     private int emptyLineCount=-1;
16     //代码行
17     private int codeLineCount=-1;
18     //注释行
19     private int commentLineCount=-1;
20 
21     public String getInputFileName() {
22         return inputFileName;
23     }
24 
25     public void setInputFileName(String inputFileName) {
26         this.inputFileName = inputFileName;
27     }
28 
29     public String getOutPutFileName() {
30         return outPutFileName;
31     }
32 
33     public void setOutPutFileName(String outPutFileName) {
34         this.outPutFileName = outPutFileName;
35     }
36 
37     public int getCharCount() {
38         return charCount;
39     }
40 
41     public void setCharCount(int charCount) {
42         this.charCount = charCount;
43     }
44 
45     public int getWordCount() {
46         return WordCount;
47     }
48 
49     public void setWordCount(int wordCount) {
50         WordCount = wordCount;
51     }
52 
53     public int getLineCount() {
54         return lineCount;
55     }
56 
57     public void setLineCount(int lineCount) {
58         this.lineCount = lineCount;
59     }
60 
61     public int getEmptyLineCount() {
62         return emptyLineCount;
63     }
64 
65     public void setEmptyLineCount(int emptyLineCount) {
66         this.emptyLineCount = emptyLineCount;
67     }
68 
69     public int getCodeLineCount() {
70         return codeLineCount;
71     }
72 
73     public void setCodeLineCount(int codeLineCount) {
74         this.codeLineCount = codeLineCount;
75     }
76 
77     public int getCommentLineCount() {
78         return commentLineCount;
79     }
80 
81     public void setCommentLineCount(int commentLineCount) {
82         this.commentLineCount = commentLineCount;
83     }
84 }
View Code

WordCount:

  1 import java.io.*;
  2 import java.util.ArrayList;
  3 import java.util.List;
  4 
  5 public class WordCount {
  6 
  7     public Parameter parameter;
  8 
  9     public WordCount(String longparas) throws IOException {
 10         this.parameter = Tools.putParasToParameter(longparas);
 11     }
 12 
 13     /**
 14      * @Auther: kevin
 15      * @param
 16      * @return: void
 17      * @Descriptions:控制整个流程,单个文件处理还是多文件的递归,或者调出UI。
 18      * @date: 18-10-17 下午8:47
 19      */
 20     public void processFile() throws IOException {
 21 
 22         String outPutFile = null;
 23         String inputData = null;
 24         String textData = null;
 25         Result result = null;
 26        if(parameter.isAdvance()){
 27 
 28            Client swingUI = new Client(parameter);
 29            swingUI.createAndShowGUI();
 30 
 31        }else if(parameter.isProcessMultFiles()){
 32 
 33             result = new Result();
 34             //System.out.println("size: "+parameter.getFilesName().size());
 35             for(int i=0;i<parameter.getFilesName().size();i++){
 36 
 37                 textData = Tools.text2String(new File(this.parameter.getFilesName().get(i)));
 38                 Tools.setResult(this,parameter,result,textData);
 39                 outPutFile = result.getOutPutFileName();
 40                 inputData = Tools.formatInputData(this.parameter.getFilesName().get(i),result.getCharCount(),result.getWordCount(),result.getLineCount(),result.getCodeLineCount(),result.getCommentLineCount(),result.getEmptyLineCount());
 41                 Tools.writeFile(outPutFile,inputData);
 42                 //System.out.println(outPutFile+inputData);
 43             }
 44 
 45         }else{
 46            result = new Result();
 47            textData = Tools.text2String(new File(this.parameter.getInputfileName()));
 48            Tools.setResult(this,parameter,result,textData);
 49            outPutFile = parameter.getOutPutFileName();
 50            inputData = Tools.formatInputData(parameter.getInputfileName(),result.getCharCount(),result.getWordCount(),result.getLineCount(),result.getCodeLineCount(),result.getCommentLineCount(),result.getEmptyLineCount());
 51            Tools.writeFile(outPutFile,inputData);
 52            //System.out.println(outPutFile+inputData);
 53        }
 54 
 55     }
 56 
 57     /**
 58      * @Auther: kevin
 59      * @param textData
 60      * @return: int
 61      * @Descriptions:获取字符数
 62      * @date: 18-10-17 下午8:47
 63      */
 64     public int getCharCount(String textData){
 65         int ret = -1;
 66         ret = textData.length();
 67         return ret;
 68     }
 69 
 70     /**
 71      * @Auther: kevin
 72      * @param HasStopWords
 73     * @param textData
 74      * @return: int
 75      * @Descriptions:获取词数
 76      * @date: 18-10-17 下午8:46
 77      */
 78     public int getWordCount(boolean HasStopWords,String textData){
 79 
 80         int ret = -1;
 81         String tmp[] = textData.split("\\s+|,+");
 82         List<String> list=new ArrayList<String>();
 83 
 84         for(int i=0;i<tmp.length;i++){
 85             if(!tmp[i].equals(""))
 86                 list.add(tmp[i]);
 87         }
 88 
 89         //对停用词不进行计数
 90         if(HasStopWords){
 91             //File file = new File("/home/kevin/s.txt");
 92             File file = new File(parameter.getFilterFilename());
 93             String stopWord = Tools.text2String(file);
 94             String stopWords[] = stopWord.split("\\s+");
 95             for(int i=0;i<stopWords.length;i++){
 96                 for(int j=0;j<list.size();j++){
 97                     if(stopWords[i].equalsIgnoreCase(list.get(j))){
 98                         //System.out.println(list.get(j));
 99                         list.remove(j);
100                         j--;
101                     }
102                 }
103             }
104         }
105 
106         ret = list.size();
107 
108         return ret;
109     }
110 
111     /**
112      * @Auther: kevin
113      * @param textData
114      * @return: int
115      * @Descriptions:获取行数
116      * @date: 18-10-17 下午8:46
117      */
118     public int getLineCount(String textData){
119 
120         int ret = -1;
121         ret = textData.split(System.lineSeparator()).length;
122         return ret;
123     }
124 
125     /**
126      * @Auther: kevin
127      * @param textData
128      * @return: int
129      * @Descriptions:获取空行数
130      * @date: 18-10-17 下午8:46
131      */
132     public int getEmptyLineCount(String textData){
133 
134         int ret = 0;
135         String tmp[] = textData.split(System.lineSeparator());
136         for(int i=0;i<tmp.length;i++){
137             if(tmp[i].replaceAll("[\\s\\{\\}]","").equals(""))
138                 ret++;
139         }
140         return ret;
141     }
142 
143     /**
144      * @Auther: kevin
145      * @param textData
146      * @return: int
147      * @Descriptions:获取代码行数
148      * @date: 18-10-17 下午8:46
149      */
150 
151     public int getCodeLineCount(String textData){
152         int ret = 0;
153         String tmp[] = textData.split(System.lineSeparator());
154         ret = getLineCount(textData)-getCommentLineCount(textData)-getEmptyLineCount(textData);
155         return ret;
156 
157     }
158 
159     /**
160      * @Auther: kevin
161      * @param textData
162      * @return: int
163      * @Descriptions:获取注释行数
164      * @date: 18-10-17 下午8:49
165      */
166     public int getCommentLineCount(String textData){
167 
168         int ret = 0;
169         String tmp[] = textData.split(System.lineSeparator());
170         for(int i=0;i<tmp.length;i++){
171             if(tmp[i].contains("//"))
172                 ret++;
173         }
174         return ret;
175     }
176 }
View Code

Tools:

  1 import java.io.*;
  2 import java.util.ArrayList;
  3 import java.util.List;
  4 
  5 public class Tools{
  6     //-c   字符数
  7     // -W  词数
  8     // -l  行数
  9     // -o  指定输出文件
 10     // -e  停用词表,接文件名
 11     // -a  返回更复杂的数据(代码行 / 空行 / 注释行)
 12     // -s  递归处理目录下符合条件的文件,接文件名
 13     // -x  调用高级功能
 14     public static final String DEFAULTPARAS[] = {"-c","-w","-l","-o","-e","-a","-s","-x"};//8个
 15 
 16     /*
 17     *
 18     * 将数组参数连接为字符串
 19     * */
 20     public static String junctParas(String args[]){
 21         String junction = "";
 22 
 23         for(int i=0;i<args.length;i++){
 24             junction += args[i]+" ";
 25             //System.out.println(args[i]);
 26         }
 27 
 28         return junction.trim();
 29     }
 30 
 31     /*
 32     * 将参数放进parameter
 33     *
 34     * */
 35     public static  Parameter putParasToParameter(String longParas){
 36         String opt = null;
 37         String realParas[] = getRealParas(longParas);
 38         Parameter parameter = new Parameter();
 39 
 40         for(int i=0;i<realParas.length;i++){
 41             opt = realParas[i];
 42             switch (opt){
 43                 case "-c":
 44                     parameter.setShowCharCount(true);
 45                     break;
 46                 case "-w":
 47                     parameter.setShowWordCount(true);
 48                     break;
 49                 case "-l":
 50                     parameter.setShowlineCount(true);
 51                     break;
 52                 case "-o":
 53                     parameter.setOutput(true);
 54                     parameter.setOutPutFileName(getFileName("-o",longParas));
 55                     //System.out.println(parameter.getOutPutFileName());
 56                     break;
 57                 case "-e":
 58                     parameter.setFilterWord(true);
 59                     parameter.setFilterFilename(getFileName("-e",longParas));
 60                     break;
 61                 case "-a":
 62                     parameter.setShowMore(true);
 63                     break;
 64                 case "-s":
 65                     parameter.setProcessMultFiles(true);
 66                     parameter.setFilesName(getRealFileNameFromList(getFileName("-s",longParas)));
 67                     break;
 68                 case "-x":
 69                     parameter.setAdvance(true);
 70                     break;
 71                     default:
 72             }
 73         }
 74         parameter.setInputfileName(getInputFileName(longParas));
 75 
 76         return parameter;
 77     }
 78     /*
 79     *
 80     * 解析真实的命令参数
 81     * */
 82     private static String[] getRealParas(String longParas){
 83         String tmp[] = longParas.split(" ");
 84         List<String> realPara=new ArrayList<String>();
 85         for(int i=0;i<tmp.length;i++){
 86             for(int j=0;j<DEFAULTPARAS.length;j++){
 87                 if( tmp[i].trim().equals(DEFAULTPARAS[j]) ){
 88                     realPara.add(tmp[i]);
 89                 }
 90             }
 91         }
 92 
 93         return (String[])realPara.toArray(new String[realPara.size()]);
 94     }
 95 
 96     //String canshu="-c e:\\test.txt -W -l -o -s -e file.txt";
 97     /*
 98     * 基础功能,单个输入文件名获取
 99     *
100     * */
101     private static String getInputFileName(String longParas){
102        String inputFileName = null;
103        String tmp[] = longParas.split(" ");
104        for(int i=0;i<DEFAULTPARAS.length;i++){
105            for(int j=0;j<tmp.length;j++){
106                if(!DEFAULTPARAS[i].equals(tmp[j]) && tmp[j].length()>2 ){
107                    //System.out.println(tmp[j]+" "+DEFAULTPARAS[i]);
108                    if( !tmp[j].equals(getFileName("-o",longParas)) && !tmp[j].equals(getFileName("-e",longParas)) && !tmp[j].equals(getFileName("-s",longParas))){
109                        //System.out.println(getFileName("-o",longParas)+getFileName("-e",longParas)+getFileName("-s",longParas));
110                        inputFileName = tmp[j];
111                    }
112                }
113            }
114        }
115 
116         return inputFileName;
117     }
118 
119     /*
120     *
121     * 通过命令参数获取 所需文件,如: 停用词表,输出文件
122     *
123     * */
124     private static String getFileName(String para,String paras){
125         boolean isNewFile = paras.contains(para);
126         String newFileName = null;
127         String str[] = paras.substring(paras.indexOf(para)+3).split(" ");
128         if(isNewFile){ newFileName = str[0];}
129         return newFileName;
130     }
131 
132     /*
133     * 筛选需要用到的文件,递归做匹配相应的文件.
134     *
135     * */
136     public static List<String> getRealFileNameFromList(String filePath){
137         String processedFile = getProcessedFileName(filePath);
138         String diviPath[] = getFileNameFormPath(filePath);
139 
140         List<String> fileList = getFileNameFromDir(new File(diviPath[0]));
141         for(int i=0;i<fileList.size();i++){
142             //System.out.println(fileList.size());
143             String fileName = getFileNameFormPath(fileList.get(i))[1];
144             //System.out.println(diviPath[0]+" "+processedFile);
145             if(!fileName.matches(processedFile) && !diviPath[0].equals(processedFile)){//当文件名匹配不成功成功时,移除不符合规则文件
146                 fileList.remove(i);
147                 i--;
148             }
149         }
150 
151         return fileList;
152     }
153 
154     /*
155     *
156     * 对文件名进行通配符解释
157     *
158     * */
159     private static String getProcessedFileName(String filePath){
160 
161         String file[] = getFileNameFormPath(filePath);
162         file[1] = file[1].replace("*",".*");//匹配任意一个或多个字符
163         file[1] = file[1].replace("?",".{1,1}");//匹配任意一个字符
164         //file[1] = file[0]+file[1];
165         //System.out.println(file[0]+"6"+file[1]);
166         return file[1];
167     }
168 
169     /*
170     *
171     * 从路径中分离文件名和文件夹名,数组索引0为文件夹,数组索引1为文件
172     * */
173     private static String[] getFileNameFormPath(String filePath){
174         String ret[] = new String[2];
175         if(filePath.contains("/")){
176             int index = filePath.lastIndexOf("/");
177             int len = filePath.length();
178 
179             ret[0] = filePath;
180             ret[1] = filePath;
181 
182             if(len != index+1){
183                 String tmp = filePath.substring(index+1);
184                 if(tmp.matches(".*\\.\\S*")) {
185                     ret[0] = filePath.substring(0, index + 1);
186                     ret[1] = filePath.substring(index + 1);
187                 }
188             }
189         }else{
190             File directory = new File("");
191             ret[0] = directory.getAbsolutePath();
192             ret[1] = filePath;
193         }
194         return ret;
195     }
196 
197     /*
198     *
199     * 获取文件件下面所有文件
200     * */
201     private static List<String> getFileNameFromDir(File file){
202         List<String> fileList = new ArrayList<String>();
203         addFileNameToList(file,fileList);
204         return fileList;
205     }
206 
207     /*
208     *
209     * 通过递归把文件加入List
210     * */
211     private static void addFileNameToList(File file, List<String> fileList){
212 
213         //System.out.println(file);
214         File files[] = file.listFiles();
215 
216         if(files!=null) {
217             for (int i = 0; i < files.length; i++) {
218                 //System.out.println(files.length);
219                 if (files[i].isFile()) {
220 
221                     fileList.add(files[i].toString());
222 
223                 } else if (files[i].isDirectory()) {
224 
225                     File tmp = new File(files[i].toString());
226                     addFileNameToList(tmp, fileList);
227 
228                 }
229             }
230         }
231     }
232 
233     /*
234     *
235     * 从文件中读取数据转换为String类型数据
236     * */
237     public static String text2String(File file){
238         if(!file.exists())return null;
239         StringBuilder result = new StringBuilder();
240 
241         try{
242             BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
243             String s = null;
244             while((s = br.readLine())!=null){//使用readLine方法,一次读一行
245 
246                 result.append(s+System.lineSeparator());
247                 //System.out.println("+");
248             }
249             br.close();
250         }catch(Exception e){
251             e.printStackTrace();
252         }
253         return result.toString();
254     }
255 
256     /*
257     *
258     * 对所有结果进行统一设置
259     * */
260     public static void setResult(WordCount wordCount,Parameter parameter,Result result,String textData){
261 
262         //System.out.println(wordCount);
263         //字符计数
264         if(parameter.isShowCharCount()){ result.setCharCount(wordCount.getCharCount(textData)); }
265         //词数计数
266         //System.out.println(result);
267         if(parameter.isShowWordCount()){ result.setWordCount(wordCount.getWordCount(parameter.isFilterWord(),textData)); }
268 
269         //总行计数
270         if(parameter.isShowlineCount()){ result.setLineCount(wordCount.getLineCount(textData)); }
271 
272         //代码行,注释行,空行计数
273         if(parameter.isShowMore()){
274             result.setCodeLineCount(wordCount.getCodeLineCount(textData));
275             result.setCommentLineCount(wordCount.getCommentLineCount(textData));
276             result.setEmptyLineCount(wordCount.getEmptyLineCount(textData));
277         }
278         //输出文件名
279         if(parameter.isOutput()) result.setOutPutFileName(parameter.getOutPutFileName());
280         else result.setOutPutFileName("result.txt");
281     }
282 
283     /*
284     *
285     * 格式化输出结果
286     * */
287     public static String formatInputData(String fileName,int charCount,int wordCount,int lineCount,int codeLineCount,int commentLineCount,int emptyLineCount){
288         String ret = "文件名路径: "+fileName+" 字符数: "+ String.valueOf(charCount)+" 词数: "+String.valueOf(wordCount)+" 总行数: "+String.valueOf(lineCount)+" 代码行数: "+String.valueOf(codeLineCount)+" 注释行数: "+String.valueOf(commentLineCount)+" 空行数:"+String.valueOf(emptyLineCount);
289         return ret;
290     }
291 
292     /*
293     *
294     * 将数据写入文件
295     * */
296     public static void writeFile(String fileName,String data){
297         File file = new File(fileName);// 要写入的文件路径
298         if (!file.exists()) {// 判断文件是否存在
299             try {
300                 file.createNewFile();// 如果文件不存在创建文件
301                 System.out.println("文件"+file.getName()+"不存在已为您创建!");
302             } catch (IOException e) {
303                 System.out.println("创建文件异常!");
304                 e.printStackTrace();
305             }
306         } else {
307             System.out.println("文件"+file.getName()+"已存在!");
308         }
309 
310 
311         FileOutputStream fos = null;
312         PrintStream ps = null;
313         try {
314             fos = new FileOutputStream(file,true);// 文件输出流    追加
315             ps = new PrintStream(fos);
316         } catch (FileNotFoundException e) {
317             e.printStackTrace();
318         }
319         String string  = data+"\r\n";// +换行
320         ps.print(string); // 执行写操作
321         ps.close();    // 关闭流
322 
323         System.out.println("文件写入完毕!");
324     }
325 }
View Code

另外单元测试,也做了相应的函数测试:

界面:

Tools(太多就贴两张图):

WordCount(太多就贴两张图):

 

五.总结:

  这次结对编程给我很大的影响。首先通过这次作业学到的不仅仅是编程的部分,合作也是之间相互协调很重要。

            这个成立: 1+1>2

另外在讨论中我们都能相互学习到对方不了解的领域。另外编程部分了解一些工具的使用,学会一点皮毛的单元测

试,一些编码规范也同时了解到一些。

  这次编码不是时间用的最多的,反而是两个人之间代码相互融合,以及对函数的缺陷的修改。当然此次项目的

结果还是有很多bug的,所以再之后我们还会抽时间继续完善一些函数,另外做到优化性能。这次合作项目只是

简单的开始,”路漫漫其修远兮,吾将上下而求索“。此后的时间里我会更加注重基础,因在这个项目中给我影响最

大是java自带的函数之强大,让我感受到了算法之美!!!ps:贴上自己的WordCount

posted @ 2018-10-17 21:50  SirPi  阅读(250)  评论(0编辑  收藏  举报