import java.io.*;
import java.util.*;
import java.lang.Character.UnicodeBlock;
/**
* Count and display for the specified input file
* the number of characters contained in various Unicode blocks .
* @author Diomidis Spinellis
*/
class CharCount {
public static void main(String args[]) {
if (args.length != 2) {
System.err.println("Usage: CharCount file encoding");
System.exit(1);
}
// Open file; try-with-resources
try (var in = new BufferedReader(new InputStreamReader(
new FileInputStream(args[0]), args[1]))) {
// Count characters in blocks
var count = new HashMap<Character.UnicodeBlock, Integer>();
int c;
while ((c = in.read()) != -1) {
var u = Character.UnicodeBlock.of(c);
Integer oldN = count.get(u);
if (oldN == null)
count.put(u, 1);
else
count.put(u, oldN + 1);
}
// Display results
for (var s : count.entrySet())
System.out.println(s.getKey() + ": " + s.getValue());
} catch (FileNotFoundException e) {
System.err.println("Unable to open file " + args[0] + ": " + e.getMessage());
System.exit(1);
} catch (UnsupportedEncodingException e) {
System.err.println("Unsupported encoding " + args[1] + ": " + e.getMessage());
} catch (IOException e) {
System.err.println("Error reading character: " + e.getMessage());
System.exit(1);
}
}
}