countspace.java

来自「nanjing university cs 的java课件。 对新手很有用。付」· Java 代码 · 共 22 行

JAVA
22
字号
import java.io.*; //计算文件中的字符数和空白字符数

class CountSpace {
	public static void main(String[] args) throws IOException {
		InputStream in;
		if (args.length == 0) {
			in = System.in;
		}
		else {
			in = new FileInputStream(args[0]);
		}
		int ch;
		int total;
		int spaces = 0;
		for (total = 0; (ch = in.read()) != -1; total++) {
			if (Character.isSpace( (char) ch)) {
				spaces++;
			}
			System.out.println(total + "chars," + spaces + "spaces"); 
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?