1 /* 2 * File: BufferedTextIOTest.java 3 * 4 * Buffered character stream I/O with BufferedReader 5 * and BufferedWriter classes 6 * 7 * Copyright: Northeast Parallel Architectures Center 8 * 9 */ 10 11 import java.io.IOException; 12 import java.io.FileWriter; 13 import java.io.BufferedWriter; 14 import java.io.PrintWriter; 15 import java.io.FileReader; 16 import java.io.BufferedReader; 17 18 public class BufferedTextIOTest { 19 20 public static void main( String args[] ) throws IOException { 21 22 String line; // for input 23 24 String filename = "BufferedTextIOTest.dat"; 25 26 String address[] = { 27 "Northeast Parallel Architectures Center", 28 "111 College Place", 29 "Syracuse University", 30 "Syracuse, NY 13244-4100" 31 }; 32 33 PrintWriter out = 34 new PrintWriter( 35 new BufferedWriter( 36 new FileWriter( filename ) ) ); // a character stream 37 38 System.out.println( "Writing..." ); 39 for ( int i = 0; i < address.length; i++ ) { 40 out.println( address[i] ); 41 } 42 43 out.close(); 44 45 BufferedReader in = 46 new BufferedReader( 47 new FileReader( filename ) ); // a character stream 48 49 System.out.println( "Reading..." ); 50 while ( ( line = in.readLine() ) != null ) { 51 System.out.println( line ); 52 } 53 54 in.close(); 55 56 } 57 58 }