// simple example of counting characters in a file // takes file name as command line argument import java.io.*; public class CharCount { public static void main(String[] args) { if (args.length == 0) { System.err.println("no files specified"); } else { System.out.println(charCount(args[0])); } } public static int charCount(String fileName) { int count = 0; try { BufferedReader fileIn = new BufferedReader(new FileReader(fileName)); while (fileIn.read() >= 0) ++count; fileIn.close(); return count; } catch (FileNotFoundException e) { System.err.println("cannot open " + fileName); System.exit(1); } catch (IOException e ) { System.err.println(e); } return count; } }