Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Davenport Spring 2005

Similar presentations


Presentation on theme: "David Davenport Spring 2005"— Presentation transcript:

1 David Davenport Spring 2005
CS102 – Files & Streams David Davenport Spring 2005

2 Input / Output in Java In java.io package File class
refers to individual file/folder (but does not do I/O) Java abstracts I/O to Streams Sequential IO (inc. files) Can only read or write (not modify) Character vs. Byte (Text vs. Binary) RandomAccessFile File class gives info about a file or folder. Eg. Is it read only, is it a file or a folder, if a folder what files does it contain, last modified, etc.

3 Sequential I/O Reading from a stream… Writing to a stream…
open stream to read from while stream contains more data read & process some data from stream close stream Writing to a stream… open stream to write to // destroys any existing data! while there is more data to write write data to stream close stream // may flush first! Standard streams: in, out, err In, out, err normally “connected” to keyboard and console but can be redirected (as in StdIO class.) In is an InputStream (probably actually a DataInputStream object) & out/err are PrintStream Normally opening a file to write to destroys all existing data, but FileWriter also allows append mode.

4 Character Streams For Unicode “Text” I/O
FileReader & FileWriter classes read/write Unicode InputStreamReader & OutputStreamWriter classes read/write native 8-bit encoded files! Only char/String, use PrintWriter for any type Use Buffered classes for efficiency As far as I understand, these streams are purely Unicode and only read/write char & Strings. The InputStreamReader & OutputStreamWriter classes provide conversion from/to native 8 bit encoding of local machine. PrintWriter will allow any type (primitive too) to be read/written.

5 Example – Character Streams
FileWriter file = new FileWriter( “temp.txt”); PrintWriter output = new PrintWriter( file); output.println( “This is some text”); output.println( 27.3); output.println( True); output.close(); FileReader file = new FileReader( “temp.txt”); BufferedReader input = new BufferedReader ( file); String aLine = input.readLine(); while ( aLine != null ) { System.out.println( aLine); aLine = input.readLine(); } input.close(); Note: without using PrintWriter you could only write String or char’s not boolean, double, etc. BufferedReader cannot read other data types! Also, it does not produce exceptions when reaching end of file!

6 Byte Streams For binary I/O
Generally use DataInputStream & DataOutputStream, with FileInoutStream & FileOutputStream classes Use Buffered classes for efficiency use PrintStream to convert to text output ObjectInputStream & ObjectOutputStream provide Object serialization readLine() in DataInputStream…           Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method.

7 Example – Binary Streams
FileOutputStream file = new FileOutputStream( “temp.txt”); BufferedOutputStream boutput = new BufferedOutputStream ( file); DataOutputStream output = new DataOutputStream ( boutput); output.writeUTF( “This is some text”); output.writeDouble( 27.3); output.writeBoolean( True); output.close(); FileInputStream file = new FileInputStream( “temp.txt”); BufferedInputStream binput = new BufferedInputStream ( file); DataInputStream input = new DataInputStream( binput); String s = input.readUTF(); double d = input.readDouble(); boolean b = input.readBoolean(); System.out.println( s + “ ” d + “ ” + b ); input.close();

8 RandomAccessFiles Can read & write (i.e. modify) files
Can seek (go to) any byte in file Bytes numbered from zero Need to be aware of data & size! (everything is just 1’s & 0’s)

9 Example – RandomAccessFiles
// Demo append to file using Java RandomAccessFile import java.io.*; public class RandomAccessFileDemo { public static void appendTo( String fileName ) throws IOException { System.out.println(" "); RandomAccessFile f = new RandomAccessFile( fileName, "rw"); f.seek( f.length() ); f.writeBytes( "This should be appended to the file, I hope!\r\n"); f.close(); System.out.println(" \n"); DataInputStream ff = new DataInputStream( new FileInputStream( fileName)); int c = ff.read(); while (c != -1) { System.out.println( c ); c = ff.read(); } ff.close(); public static void main( String[] args) throws IOException { try { appendTo( "appendtest.dat"); System.out.println( " .. Done .. "); } catch ( Exception e) { System.out.println(e); System.in.read();

10 ToDo OUTPUT streams MUST be closed else may lose data! add few notes about different files types (sequential and Random Access) and about different Streams (byte vs. character) byte streams are byte oriented, character streams are unicode and about data vs processed source can be file, string, etc. and passed raw or filtered/converted Readers/Writers are unicode based. Standard IO streams… in, out, err. Serialization


Download ppt "David Davenport Spring 2005"

Similar presentations


Ads by Google