软件工程项目程序:WC

1:代码来源:http://yuncode.net/code/c_5087c8e4cd77190

2:Platform:Eclipse

Language:Java

3:Bug:暂时没有

4. Function improvement:实现对文件里的字符,单词以及行数的统计。

5:代码:

import java.io.*;

/**
* 统计文本文件的行数,单词书,字节数
*/
class WordCount {
public static int words = 1;
public static int lines = 1;
public static int chars = 0;

public static void wc(InputStream f) throws IOException {
int c = 0;
boolean lastNotWhite = false;
String whiteSpace = " \t\n\r";
while ((c = f.read()) != -1) {
chars++;
if (c == '\n') {
lines++;
}
if (whiteSpace.indexOf(c) != -1) {
if (lastNotWhite) {
words++;
}
lastNotWhite = false;
} else {
lastNotWhite = true;
}
}
}

public static void main(String args[]) {
FileInputStream f;
try {
if (args.length == 0) { // We're working with stdin
f = new FileInputStream("c:/123.txt");
wc(f);
} else { // We're working with a list of files
for (int i = 0; i < args.length; i++) {
f = new FileInputStream(args[i]);
wc(f);
}
}
} catch (IOException e) {
return;
}
System.out.println(lines + "行 " + words + "个单词 " + chars + "个字节");
}
}

6:github地址:https://github.com/yeershao/hello-world/commit/9e6930db18fd409a3513318ed922d4fb67bb19a1

上一篇:2017-2018-2 20165315 实验三《敏捷开发与XP实践》实验报告


下一篇:Java集合——Map接口