Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Writing in files

Similar presentations


Presentation on theme: "Object Writing in files"— Presentation transcript:

1 Object Writing in files
Sangeetha Parthasarathy 07/17/2001

2 File Writer Convenience class for writing character files. The
constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. Class FileWriter java.lang.Object| +-java.io.Writer| +-java.io.OutputStreamWriter| + -java.io.FileWriter

3 Methods in FileWriter public FileWriter(String fileName) throws IOException public FileWriter(String fileName, boolean append) throws IOException public FileWriter(File file) throws IOException public void write(int c) throws IOException Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high- rder bits are ignored. Subclasses that intend to support efficient single-character output should override this method. Throws: IOException - If an I/O error occurs

4 public void write(char[] cbuf) throwsIOException: Write an array of character throws: IOException- If an I/O error occurs public void write(char[] cbuf, int off, int len) throws IOException: Write a portion of an array of characters. public void write(int c) throws IOException: Write a single character. Throws: IOException- If an I/O error occurs.

5 FileReader Convenience class for reading character files.
Class FileReader java.lang.Object| +-java.io.Reader| +-java.io.InputStreamReader| +-java.io.FileReader

6 Methods of FileReader public FileReader(String fileName) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException public int read() throws IOException Read a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. public int read(char[] cbuf) throws IOException Read characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

7 public abstract int read(char[] cbuf, int off, int len) throws IOException: Read characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. Parameters: cbuf - Destination buffer, off - Offset at which to start storing characters and len - Maximum number of characters to read

8 Serialization Java’s object serialization allows you to take any object that implements the Serializable interface and turn it into a sequence of bytes that can later be fully restored to regenerate the original object. This is even true across a network, which means that the serialization mechanism automatically compensates for differences in operating systems. That is, you can create an object on a Windows machine, serialize it, and send it across the network to a Unix machine where it will be correctly reconstructed. You don’t have to worry about the data representations on the different machines, the byte ordering, or any other details.

9 ObjectOutput Stream An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Only objects that support the java.io.Serializable interface can be written to streams. The class of each serializable object is encoded including the class name and signature of the class, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects. The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written.

10 Methods of ObjectOutputStream
public ObjectOutputStream(OutputStream out) throws IOException: Creates an ObjectOutputStream that writes to the specified OutputStream. The stream header is written to the stream. public final void writeObject(Object obj) throws IOException: Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. public void write(int data) throws IOException: Writes a byte. This method will block until the byte is actually written.

11 public void write(byte[] b) throws IOException: Writes an array of bytes. This method will block until the bytes are actually written. public void write(byte[] b, int off, int len) throws IOException: Writes a sub array of bytes. public void flush() throws IOException: Flushes the stream. This will write any buffered output bytes and flush through to the underlying stream. public void close() throws IOException: Closes the stream. This method must be called to release any resources associated with the stream. public void writeBoolean(boolean data) throws IOException: Writes a boolean.

12 public void writeByte(int data) throws IOException : Writes an 8 bit byte.
public void writeShort(int data) throws IOException : Writes a 16 bit short. public void writeChar(int data) throws IOException : Writes a 16 bit char. public void writeInt(int data) throws IOException : Writes a 32 bit int. public void writeLong(long data) throws IOException : Writes a 64 bit long. public void writeFloat(float data) throws IOException : Writes a 32 bit float. public void writeDouble(double data) throws IOException : Writes a 64 bit double.

13 public void writeBytes(String data) throws IOException: Writes a String as a sequence of bytes.
public void writeChars(String data) throws IOException : Writes a String as a sequence of chars.

14 ObjectInputStream An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. ObjectInputStream is used to recover those objects previously serialized. Class ObjectInputStream java.lang.Object| +-java.io.InputStream| +-java.io.ObjectInputStream

15 Methods of ObjectInputStream
public ObjectInputStream(InputStream in) throws IOException: Create an ObjectInputStream that reads from the specified InputStream. public int available() throws IOException: Returns the number of bytes that can be read without blocking. public void close() throws IOException: Closes the input stream. Must be called to release any resources associated with the stream. public final ObjectreadObject(): Read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read.

16 public int read() throws IOException: Reads a byte of data
public int read() throws IOException: Reads a byte of data. This method will block if no input is available. public int read(byte[] b, int off, int len) throws IOException: Reads into an array of bytes. This method will block until some input is available. Consider using java.io.DataInputStream.readFully to read exactly 'length' bytes. public boolean readBoolean() throws IOException: Reads in a boolean. public byte readByte() throws IOException: Reads an 8 bit byte. public short readShort() throws IOException: Reads a 16 bit short. public int readUnsignedShort() throws IOException: Reads an unsigned 16 bit short.

17 public char readChar() throws IOException: Reads a 16 bit char.
public int readInt() throws IOException: Reads a 32 bit int. public long readLong() throws IOException: Reads a 64 bit long. public float readFloat() throws IOException: Reads a 32 bit float. public double readDouble() throws IOException: Reads a 64 bit double. public String readLine() throws IOException: Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives. Reads in a line that has been terminated by a \n, \r, \r\n or EOF.


Download ppt "Object Writing in files"

Similar presentations


Ads by Google