第二次作业

Gitee地址:https://gitee.com/tyx666666/wordcount.git
1.功能分析

  WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。可执行程序命名为:wc.exe,该程序处理用户需求的模式为:

wc.exe [parameter] [input_file_name]

2.思路
首先要有对java文件处理的,因为要读文件还要写文件;

对字符进行统计,就要会一些正则表达式去处理这些字符串;

为了便于管理项目还要将项目推到GitHub上;

3.程序设计
第一次使用git和码云弄了半天,说实话感觉不是很友好

  public class WordCount {

// -c
public 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;
}

// -w
public 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;
}

//-l
public 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;
}

// -o
public 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<args.length;i++){
		//字符数
		if(args[i].equals("-c")){
			func[0] = true;
		}
		//单词数
		else if(args[i].equals("-w")){
			func[1] = true;
		}
		//行数
		else if(args[i].equals("-l")){
			func[2] = true;
		}
		//输出
		else if(args[i].equals("-o")){
			if(i==(args.length-1)) {
				System.out.println("‘-o’后必须跟文件名。");
				func[6] = false;
			}
			else {
				func[6] = true;
				outpath = args[++i];
			}
		}
		else{
			inpath = args[i];
		}
	}
	
	//实现程序功能
    if(func[0]){
    	//-c
   		output += fileChars(inpath);
   	}
   	if(func[1]){
    	//-w
    	output += fileWords(inpath);


    }
    if(func[2]){
    	//-l
    	output += fileLines(inpath);
    }
    if(func[6]){
       	//-o
       	outPut(output,outpath);
    }
        //如果没有指定输出文件,则输出到result.txt
    else {
        outPut(output,"result.txt");
    }
	System.out.println(output);
}

}

四.测试


好像是没问题了

五.总结
经验不足,也没有好好学习,参考了一些实例....

参考:一些实例https://www.cnblogs.com/lu0526/p/9690691.html

posted @ 2018-09-24 17:40  iiiiiiCode  阅读(117)  评论(0编辑  收藏  举报