博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第二次作业
阅读量:4332 次
发布时间:2019-06-06

本文共 3733 字,大约阅读时间需要 12 分钟。

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

}

四.测试

1497155-20180924172647204-116128994.png

好像是没问题了

五.总结

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

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

转载于:https://www.cnblogs.com/tyx666/p/9696051.html

你可能感兴趣的文章
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>