40 import java.io.BufferedReader;
41 import java.io.FileNotFoundException;
42 import java.io.FileReader;
43 import java.io.IOException;
44 import java.util.function.Consumer;
45 import java.util.regex.Pattern;
75 public static void main(String[] args)
throws IOException {
77 if (args.length != 1) {
82 try (BufferedReader reader =
new BufferedReader(
83 new FileReader(args[0]))) {
97 }
catch (FileNotFoundException e) {
99 System.err.println(e);
111 System.out.println(
"Character count = "
112 + reader.lines().flatMapToInt(String::chars).count());
121 System.out.println(
"Word count = "
123 .flatMap(nonWordPattern::splitAsStream)
124 .filter(str -> !str.isEmpty()).count());
127 System.out.println(
"Newline count = " + reader.lines().count());
134 System.out.println(
"Max line length = "
135 + reader.lines().mapToInt(String::length).max().getAsInt());
157 .collect(WCStatistics::new,
160 System.out.println(wc);
164 System.out.println(
"Usage: " +
WC.class.getSimpleName() +
" FILE");
165 System.out.println(
"Print newline, word,"
166 +
" character counts and max line length for FILE.");
192 .filter(str -> !str.isEmpty()).count();
208 StringBuilder sb =
new StringBuilder();
209 sb.append(
"#------WCStatistic------#\n");
210 sb.append(
"Character count = ").append(
characterCount).append(
'\n');
211 sb.append(
"Word count = ").append(
wordCount).append(
'\n');
212 sb.append(
"Newline count = ").append(
lineCount).append(
'\n');
213 sb.append(
"Max line length = ").append(
maxLineLength).append(
'\n');
214 return sb.toString();