1 / 13

Mark and Reset

Mark and Reset. The abstract classes InputStream and Reader provide: void mark (int byteLimit) * Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

jean
Download Presentation

Mark and Reset

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. Mark and Reset The abstract classes InputStream and Reader provide: void mark (int byteLimit) * Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. * byteLimit is the number of bytes that can be read before this mark becomes invalid void reset() * repositions stream to position when mark was last called boolean markSupported () indicates if a stream supports these methods

  2. int[] intList; intList = new int[100]; //this file contains one number 66 FileReader file = new FileReader("number.txt"); BufferedReader ifile = new BufferedReader(file); // all array elements are initialized to 66 ifile.mark(5); for (int i = 0; i<100; i++){ String sVal = ifile.readLine(); ifile.reset(); intList[i] = Integer.parseInt(sVal); }

  3. Object Output Another byte (binary) I/O class which extends from OutputStream is ObjectOutputStream ObjectOutputStream class can save entire objects to disk !

  4. Serializable • Objects that are written to an object stream must belong to a class that implements the Serializable interface. class Coin implements Serializable { ... } ** Serializable interface has no methods.

  5. ObjectOutputStream methods Stream does not just write objects …….. write( ) - writes a byte writeInt(int) - writes a 32 bit int writeDouble(double) - writes a 64 bit double writeChar(char) - writes a 16 bit char writeObject(Object) - writes the specified object to the output stream (if Serializable) close() - closes stream

  6. public static void main (String[] args) throws IOException{ int iVal = 15; int[] oVal; oVal = new int[10]; //init some primitive and an Array object double dVal = 55.6; for (int j = 0; j< 10; j++) oVal[j] = j; //set up output file for receiving objects OutputStream ofile = new FileOutputStream("data.dat"); ObjectOutputStream ostrm = new ObjectOutputStream(ofile); //output the data ostrm.writeInt(iVal); ostrm.writeObject(oVal); //Java arrays implement Serializable ostrm.writeDouble(dVal); //close file ostrm.close(); } Writing an Object to a File

  7. Object Input • Another byte (binary) I/O class which extends from InputStream is ObjectInputStream ObjectInputStream class can read from a file, written by ObjectOutputStream (Data must by read with respect to datatype and order with which it was written)

  8. ObjectInputStream methods read( ) - reads a byte readInt() - reads a 32 bit int readDouble() - reads a 64 bit double readChar() - reads a 16 bit char readObject( ) - reads the specified object to the input stream (if Serializable) close() - closes stream

  9. public static void main (String[] args) throws IOException, ClassNotFoundException { int iVal ; int[] oVal; double dVal ; //set up output file for receiving objects InputStream ifile = new FileOutputStream("data.dat"); ObjectInputStream istrm = new ObjectInputStream(ifile); //output the data iVal = istrm.readInt(); oVal = (int []) istrm.readObject(); dVal =istrm.readDouble(); // data has been read into variables //close file ostrm.close(); } Reading from File containing Objects

  10. Reading a Coin Object from a File ObjectInputStream in = new ObjectInputStream(newFileInputStream("coins.dat")); Coin c = (Coin)in.readObject();

  11. Random .vs. Sequential Access • Sequential access • A file is processed a byte at a time. • Random access • Allows access at arbitrary locations in the file

  12. Random and Sequential Access To open a random-access file for reading and writing RandomAccessFile f = new RandomAcessFile("bank.dat","rw");

  13. To move the file pointer to a specific byte f.seek(n); • To get the current position of the file pointer. long n = f.getFilePointer(); • To find the number of bytes in a file long fileLength = f.length();

More Related