Thursday, August 12, 2010

Java - Files and Streams Summary Part 1

1.1  Introduction.
Java.io package provides an extensive library of classes to deal with input and output. Java provides streams as a general mechanism for dealing with data  I/O.

There are three kinds of streams.

  1. Byte Streams 
  2. Character Streams
  3. Buffered Streams
An input stream - an object that an application use to read sequence of data, acts as a source of data.
An output stream - an object that an application use to write a sequence of data, acts as a destination of data.

Entities work as output or input streams.

  1. An array of character or bytes.
  2. A file
  3. A pipe 
  4. A network connection
1.2 File Class

File(String pathname)
          Creates a new File instance by converting the given pathname string into an abstract pathname.

Methods

  1. String getName() - Returns the name of the file, excluding directory file resides.
  2. String getPath() - Returns the (absolute or relative) pathname of the file.
  3. String getAbsolutePath() - Returns the absolute pathname string of this abstract pathname.
  4. Stirng getCononicalPath() - Platform-dependent. Returns the canonical form of this abstract pathname.
  5. String getParent() -  The parent part of the pathname of this File object is returned if one exists, otherwise the null value is returned.
  6. boolean isAbsolute() - Whether a File object represents an absolute pathname can be determined using this method.
  7. long lastModified() - Returns the time that the file denoted by this abstract pathname was last modified.
  8. long length() - Returns the size (in bytes) of the file represented by the File object.
  9. boolean equals(Object obj) - This method only compares the pathnames of the File objects, and returns true if they are identical.
  10. boolean exists() - Return true if specified file or directory is existed.
  11. boolean isFile() - Return true if the object is a file.
  12. boolean isDirectory() - Returns true if the object is a directory.
  13. boolean canRead() - Returns true if the object has read access.
  14. boolean canWrite() - Returns true if the object has writing access.
  15. String[] list() - Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
  16. String[] list(FileNameFilter filter) -  Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
1.3 Creating New Files and Directories
  1. boolean  createNewFile() throws IOException  - Creates  a new, empty file named by the abstract pathname if,and only if, a file with this name does not already exist. Return value is true if the file is created, else false means file is already exists.
  2. boolean mkdir() - Creates the directory named by this abstract pathname.
  3. boolean mkdirs() - Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
1.4 Rename Files and Directories
  1. boolean renameTo(File dest) - Renames the file denoted by this abstract pathname.
1.5 Deleting Files and Directories
  1.  boolean delete() -  Deletes the file or directory denoted by this abstract pathname.

No comments:

Post a Comment