java 统计文本行数_统计文本文件的行数,单词书,字节数
[java]代碼庫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 + "個字節");
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java 统计文本行数_统计文本文件的行数,单词书,字节数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中junit_【Java】Jun
- 下一篇: java常见业务对象_Java各种对象(