1. 代码来源:
博客园http://www.cnblogs.com/duasonir/p/5297338.html
2. Platform:Eclipse
Language:Java
3. Bug:暂时还没有bug
4. Function improvement:所找的代码的各个扩展已经很全面,主函数用string字符串接受用户输入,并分解成参数数组和文件地址。编写BaseCount()函数实现对文件的读操作,逐行统计统计字符数,并记录行数,同时使用StringBuffer类记录文件中所有的信息。最后一起统计词数,避免在逐行计词时,把标点计为一词。编写Response()函数根据用户输入的参数输出信息,这里程序不管用户输入的参数是什么,都在读取文件的时候把所有信息都记录下来并保存,用户输入的参数里有什么输出什么。
5. Implementation:可实现
6.代码如下:
package cWordCount; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Scanner; public class wcXiangMu { private static String shuru = ""; private static String[] strLong; private static String File; private static int linecount = 0; private static int wordcount = 0; private static int charcount = 0; private static int nullLinecount = 0; private static int codeLinecount = 0; private static int noteLinecount = 0; public static void main(String[] args) { while(true){ System.out.print("wc.exe "); Scanner s = new Scanner(System.in); shuru = s.nextLine(); String[] arrMessSplit = shuru.split(" "); int iMessLength = arrMessSplit.length; strLong = new String[iMessLength - 1]; File = arrMessSplit[iMessLength - 1]; CountNumber(); for (int i = 0; i < strLong.length; i++) { if (arrMessSplit[i].equals("-w")){ System.out.println("单词数:"+wordcount); } if (arrMessSplit[i].equals("-l")){ System.out.println("文件总行数:"+linecount); } if (arrMessSplit[i].equals("-c")){ System.out.println("字符数:"+charcount); } if (arrMessSplit[i].equals("-a")){ System.out.println("空行数:"+nullLinecount); System.out.println("代码行数:"+codeLinecount); System.out.println("注释行数:"+noteLinecount); } } } } private static void CountNumber() { linecount = 0; wordcount = 0; charcount = 0; nullLinecount = 0; codeLinecount = 0; noteLinecount = 0; File file = new File(File); if (file.exists()) { try { FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader br = new BufferedReader(isr); String line = ""; StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null) { linecount++; sb.append(line); charcount += line.length(); line = line.trim(); if (line == "" || line.length() <= 1) { nullLinecount++; continue; } int a = line.indexOf("/"); int b = line.substring(a + 1).indexOf("/"); if (b == 0) { noteLinecount++; continue; } codeLinecount++; } wordcount = sb.toString().split("\\s+").length; br.close(); isr.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("path error"); } } }
7. Github:https://github.com/I-December/WC-project
时间: 2024-11-22 23:14:37