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