/* program name = bufferedFile.java author : Michael Robinson purpose : Use BufferedWriter, FileWriter BufferedReader, FileReader To write and read files in Java Updated : Jan 25th, 2014 */ import java.io.*; public class bufferedFile { public static void processFile() throws IOException { String tokens[] = new String[2]; int x = 0; BufferedWriter outFile = new BufferedWriter( new FileWriter( "testFile" ) ); for( x = 0; x < 5; x++) { outFile.write( x + " " + 0 + "\n" ); //write newRecord to outFile file } for( x = 5; x > 0; x--) { outFile.write( 0 + " " + x + "\n" ); //write newRecord to outFile file } outFile.close(); BufferedReader inFile = new BufferedReader( new FileReader( "testFile" ) ); String record = inFile.readLine(); while( record != null ) { tokens = record.split(" "); if( tokens[0].compareTo( tokens[1] ) == 0 ) //both fields are equal { System.out.println( tokens[0] + " is equal to " + tokens[1] ); } else if( tokens[0].compareTo( tokens[1] ) > 0 ) // tokens[0] > tokens[1] { System.out.println( tokens[0] + " is greater than " + tokens[1] ); } else if( tokens[0].compareTo( tokens[1] ) < 0 ) // tokens[0] < tokens[1] { System.out.println( tokens[1] + " is less than " + tokens[0] ); } record = inFile.readLine(); }//end while( record != null ) inFile.close(); }//end processFile() public static void main( String arg[] ) throws IOException { processFile(); }//end public static void main( String arg[] ) }//end bufferedFile