|
|
The FileStreams example highlights theFileInputStreamandFileOutputStreamclasses. JDK 1.1 introduces two new classes,FileReaderand
FileWriter, that perform the same function as
FileInputStreamandFileOutputStreamonly they work with characters rather than bytes. Generally speaking, it is best to use the new character-streams.Here's the FileStreams example rewritten to use
FileReaderandFileWriterclasses instead ofFileInputStreamandFileOutputStream:import java.io.*; class FileStreamsTest { public static void main(String[] args) { try { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); FileReader fr = new FileReader(inputFile); FileWriter fw = new FileWriter(outputFile); int c; while ((c = fr.read()) != -1) { fw.write(c); } fr.close(); fw.close(); } catch (FileNotFoundException e) { System.err.println("FileStreamsTest: " + e); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } } }
|
|