Java Application File I/O
since May 10, 2000, last modified May 10, 2000
Java Application with File I/O
The following code reads text data in 'input.txt' file and write the result to both the std I/O and 'output.txt' file. It uses FileReader and FileWriter classes in the java.io package. See the binary I/O stream classes which respond to the classes respectively.
import java.io.*; public class fio { public static void main(String[] args) throws IOException { FileReader in = null; FileWriter out = null; try { in = new FileReader("input.txt"); out = new FileWriter("output.txt"); } catch (FileNotFoundException e) { System.err.println("CheckedIOTest: " + e); System.exit(-1); } catch (IOException e) { System.err.println("CheckedIOTest: " + e); System.exit(-1); } int c; while ((c = in.read()) != -1) { out.write(c); System.out.print((char)c); } in.close(); out.close(); } }For more information of I/O stream, see The Java Tutorial "Essential Java Classes.Reading and Writing (but No 'rithmetic)" section.
Knowledge and Engineering Databases (c) copyright Namchul Do, 2000