MY mENU


Tuesday 13 March 2012

Input and Output Streams


Some Important points on Input and OutPut Streams:
  • InputStream and OutputStream are abstract classes.
  • FileInputStream and FilterInputStream both inherit directly from InputStream.
  • FileOutputStream and FilterOutputStream both inherit directly from OutputStream.
  • DataInputStream inherits directly from FilterInputStream.
  • DataOutputStream inherits directly from FilterOutputStream.
  • When you create a Filter stream, you must specify the stream to which it will attach.
  • A Filter stream processes a stream of bytes in some way. By “chaining” any number of Filter streams, you can add any amount of processing to a stream of bytes.
  • DataInputStream, DataOutputStream and RandomAccessFile know how to work with Java data types because they implement the
  • DataInput and DataOutput interfaces, whereas FileInputStream and FileOutputStream know only how to work with individual bytes.
  • RandomAccessFile implements both DataInput and DataOutput methods - RandomAccessFile objects can read from and write to files.
  • The File class is not used to create files. Can create a file using an instance of class RandomAccessFile and FileOutputStream.
  • To test if a File object refers to an existing file, you can invoke exists() which returns true or false. The File methods canRead() and canWrite() return boolean values that indicate whether the application can read from or write to the file. (Note: applets can’t write to a file.)
  • Can use File methods to make a permanent change to the file system. For example, you can call delete() or rename(). For rename(), need to supply a File object which embodies the new name.
  • You can create a directory using the File class. The method mkdir() does this.
  • It is possible to navigate the filing system using the File class. Methods getParent(), getPath() and getName() are provided, and also getAbsolutePath().
  • The method getAbsolutePath() returns the name of the current user directory (the full pathname included) with the file name concatenated. See Practice Exam 1 Q.45
  • Creating a FileOutputStream object creates the appropriate file. If the file already exists, FileOutputStream replaces it (unless the FileOutputStream object is created using the constructor which takes a String and a boolean - see later)
  • Upon creation of a RandomAccessFile object, need to supply a file object plus a mode. Alternatively, can supply a filename plus a mode.
  • Valid modes for RandomAccessFile are “r” and “rw”.
  • Constructors for FileInputStream: one takes a String, one takes a File, one takes a FileDescriptor
  • Constructors for FileOutputStream: one takes a String, one takes a File, one takes a FileDescriptor, one takes a String and a boolean (which indicates whether or not to append).
  • Constructors for FilterInputStream: one only, which takes an InputStream
  • Constructors for FilterOutputStream: one only, which takes an OutputStream

No comments:

Post a Comment