1 / 15

Top IO Hierarchy

Top IO Hierarchy. Input Stream. Output Stream. Reader. Writer. Streams. InputStream Reader. BufferedReader std in = new BufferedReader ( new InputStreamReader ( System.in ) );. System.in is an object of InputStream. PrintWriter out = new PrintWriter ( new BufferedWriter (

palma
Download Presentation

Top IO Hierarchy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Top IO Hierarchy

  2. Input Stream

  3. Output Stream

  4. Reader

  5. Writer

  6. Streams

  7. InputStreamReader BufferedReaderstdin =new BufferedReader( new InputStreamReader( System.in ) ); System.in is an object of InputStream

  8. PrintWriterout =new PrintWriter( new BufferedWriter( newFileWriter (“foo.out” ) ) ); Note: System.out and System.err are PrintWriter objects FileReader & FileWriter BufferedReaderin =new BufferedReader( new FileReader ( “foo.in” ) );

  9. Reading from binary file FileInputStream fis = new FileInputStream(myFileObj); ObjectInputStream ois = new ObjectInputStream(fis); int i = ois.readInt(); String today = (String) ois.readObject(); Date date = (Date) ois.readObject(); ois.close();

  10. How to tell end of file try { read from file } catch ( EOFException endOfFileException ) { do what you need to do when end of file is reached } Other ways possible. This is the recommended method.

  11. Writing to binary file FileOutputStream fos = new FileOutputStream(myFileObj); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close();

  12. Reading from text file BufferedReader input = new BufferedReader( new FileReader( myFile ) ); String text = input.readLine(); input.read(myCharArray, off, len); input.close();

  13. Writing to text file BufferedWriter output = new BufferedWriter( new FileWriter( myFile ) ); String text = “hello \n”; output.write(text); output.write(myCharArray,off,len); output.flush() ; output.close();

  14. 0 100 200 300 400 500 byte offsets 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes Random Access Files • Use fixed length for every record • Easy to calculate record locations

  15. Random Access File Methods RandomAccessFile raFile = new RandomAccessFile( fileObj, "rw" ); raFile.seek( ( recordNumber - 1 ) * RandomAccessAccountRecord.SIZE ); raFile.writeInt( 12345 ); raFile.writeDouble( getBalance() ); raFile.writeChars( buffer.toString() ); int i = raFile.readInt(); double d = raFile.readDouble(); char c = raFile.readChar(); raFile.close();

More Related