High Fashioners Issue # 2 | Page 343

J. E. N. I.
12.11 Contoh Modifikasi InputStream / OutputStream
Contoh berikutnya menggunakan class PushbackInputStream yang memanfaatkan sebuah object FileInputStream dan class PrintStream.
import java. io.*;
class CopyFile { void copy( String input) { PushbackInputStream inputStr; PrintStream outputStr; int data; try { inputStr = new PushbackInputStream( new
FileInputStream( input)); outputStr = new PrintStream( System. out); while(( data = inputStr. read())!=-1) { outputStr. println(" read data: " +( char) data); inputStr. unread( data); data = inputStr. read(); outputStr. println(" unread data: " +( char) data);
} inputStr. close(); outputStr. close();
} catch( IOException ie) { ie. printStackTrace();
}
}
} public static void main( String args []) { String inputFile = args [ 0 ]; CopyFile cf = new CopyFile(); cf. copy( inputFile);
}
Uji kode ini pada sebuah file yang mengandung sedikit baris atau karakter.
Pengenalan Pemrograman 2 15