1 // CountBytes.java
2
3 import java.io.InputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6
7 /** 8 * CountBytes zeigt den Umgang mit InputStreams. 9 * @author H.Drachenfels 10 * @version 10.1.2019 11 */
12 public final class CountBytes {
13 private CountBytes() { }
14
15 /** 16 * Zählt die Bytes in einer Datei oder in der Standardeingabe. 17 * @param args optional ein Dateiname 18 * @throws IOException bei Lesefehlern 19 */
20 public static void main(String[] args) throws IOException {
21 InputStream in;
22
23 if (args.length == 0) {
24 in = System.in;
25 } else {
26 in = new FileInputStream(args[0]);
27 }
28
29 int total = 0;
30 while (in.read() != -1) {
31 ++total;
32 }
33
34 System.out.printf("%d Byte%n", total);
35 }
36 }
37