1.项目地址:https://gitee.com/monkeyjb

2.使用java实现  

 只实现了基础功能

3.代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.table.TableStringConverter;

public class WordCount {
	public static String string=null;
	public static void main(String[] args) {
		String fileName=null;
		String outFile=null;
		for(int i=0;i<args.length;i++){
		switch (args[i]) {
		case "-c":
			string=args[i+1];
			fileName=args[i+1];
			getCharCount(fileName);
			System.out.println(fileName+",字符数:"+getCharCount(fileName));	
			break;

		case "-w":
			string=args[i+1];
			fileName=args[i+1];
			getWordCount(fileName);
			System.out.println(fileName+",单词数:"+getWordCount(fileName));	
			break;
		
		case "-l":
			string=args[i+1];
			fileName=args[i+1];
			getLineCount(fileName);
			System.out.println(fileName+",行数:"+getLineCount(fileName));	
			break;
			
		case "-o":
			outFile=args[i+1];
			writeToFile(string, outFile);
			System.out.println("写入成功!");
			break;
		}
		}
		
		

	}
	
	//获取字符数量的方法
	public static int getCharCount(String fileName){
		int count=0;
		String line;
		try{
			BufferedReader bf=new BufferedReader(new FileReader(fileName));
			while((line=bf.readLine())!=null){
			count+=line.length();
		}
		bf.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	
	}
	
	//获取单词数量的方法、
	public static int getWordCount(String fileName){
		int count=0;
		String line;
		try{
			BufferedReader bf=new BufferedReader(new FileReader(fileName));
			while((line=bf.readLine())!=null){
				String[] strings=line.trim().split(" |,");
				count+=strings.length;
				
			}
			bf.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	//获取总行数的方法
	public static int getLineCount(String fileName){
		int count=0;
		String line;
		try{
			BufferedReader bf=new BufferedReader(new FileReader(fileName));
			while((line=bf.readLine())!=null){
				count++;
			}
			bf.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	//将结果写入文件的方法
	public static void writeToFile(String fileName,String outFile){
		try{
			BufferedWriter  bw=new BufferedWriter(new FileWriter(outFile));
			bw.write(fileName+",字符数:"+getCharCount(fileName));
			bw.write("\r\n");
			bw.write(fileName+",单词数:"+getWordCount(fileName));
			bw.write("\r\n");
			bw.write(fileName+",行数:"+getLineCount(fileName));
			bw.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}

  4.文件夹

5.测试

 

 不知为何会出现这样的错误,无法自己解决,暂时提交

希望日后能发现错误。

 

posted on 2018-09-24 23:28  侯江波  阅读(101)  评论(0编辑  收藏  举报