Gitee地址:
1.功能分析WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。可执行程序命名为:wc.exe,该程序处理用户需求的模式为:
wc.exe [parameter] [input_file_name]
2.思路
首先要有对java文件处理的,因为要读文件还要写文件;对字符进行统计,就要会一些正则表达式去处理这些字符串;
为了便于管理项目还要将项目推到GitHub上;
3.程序设计
第一次使用git和码云弄了半天,说实话感觉不是很友好public class WordCount {// -cpublic static String fileChars(String fileName) { File file = new File(fileName); String str = ""; int count = 0; Reader reader = null; try { // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 if (((char) tempchar) != '\r') { count++; } } str = fileName + ", 字符数:"+count +"\r\n"; reader.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return str;}// -wpublic static String fileWords(String fileName) { File file = new File(fileName); String str = ""; int count = 0;//统计单词数 int word; //是否有单词 Reader reader = null; try { // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 word = 0; while (((char) tempchar) == ' ' || ((char)tempchar) == ',' || ((char)tempchar) == '\r' ||((char)tempchar) == '\n') { word = 1; if((tempchar = reader.read()) != -1) { continue; } else { count--; } } count += word; } str = fileName + ", 单词数:"+ ++count +"\r\n"; reader.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return str;}//-lpublic static String fileLines(String fileName) { File file = new File(fileName); String str = ""; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 0; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { line++; } str = fileName + ", 行数:" + line +"\r\n"; reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return str;}// -opublic static void outPut(final String str, final String fileName){ try { File file = new File(fileName); FileWriter fileWriter = new FileWriter(file); fileWriter.write(str); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); }}public static void main(String args[]){ //循环处理文件 String inpath = ""; String outpath = ""; String output = ""; /* 是否实现某项功能的数组 * 0:-c 1:-w 2:-l 3:-a 4:-e 5:-s 6 -o */ boolean[] func = {false,false,false,false,false,false,false}; //对程序将要做哪些功能进行准备 for(int i = 0;i
}
四.测试
好像是没问题了五.总结
经验不足,也没有好好学习,参考了一些实例....参考:一些实例https://www.cnblogs.com/lu0526/p/9690691.html