/* Punters Lounge JAVA programming course * * Lesson71.java * * Author : Erik Datapunter * Date : 12/jun/2004 * * Reading from a file * */ // import the library with Input Output objects import java.io.*; public class Lesson71 { public static void main(String[ ] args) throws Exception { // create a File object pointing to a physical file File FromFile = new File("in-file.txt"); // create a FileReader object to use to read from the file FileReader ReadFile = new FileReader(FromFile); // an integer to read each character as a numerical value int ReadChar = 0; int digit = 0; int upper = 0; int lower = 0; int rest = 0; // while we have not yet reached the end of the file, read another character while ((ReadChar = ReadFile.read()) != -1) { if ( (ReadChar > 47) & (ReadChar < 57) ) digit = digit + 1; else if ( (ReadChar > 64) & (ReadChar < 89) ) upper = upper + 1; else if ( (ReadChar > 96) & (ReadChar < 121) ) lower = lower + 1; else rest = rest + 1; } System.out.println("Digits : " + digit); System.out.println("Upper case : " + upper); System.out.println("Lower Case : " + lower); System.out.println("Remaining : " + rest); // when the file is read close the stream to the file ReadFile.close(); } }