File I/o Streams

  • File is a class in package.
  • This class represents file and directorypathnames.

constructor of File class:
 File f  = new ("bala.txt");
  • the above statement dont create any file,it will check the availability of file named with bala.txt,if available then f simply represents that file.
  • If  file is not available then f  represents only name of the file without creating any physical file.
           for example:
          File f = new File("bala.txt");
           System.out.println(f.exists());//if file bala.txt not available it returns false.
           System.out.println(f.exists());// true.
  • A java File Object can represent a directorypathnames.

              File f = new File("bala");
        System.out.println(f.exists());if directory not avilable it returns false
         System.out.println(f.exists()); true

Methods of File class:

public boolean  creteNewFile() throws IOExceptio
    Automically creates a new, empty file named by this  pathname if and only if a file with this name does           not exist.true if the named file does not exist and was successfully created; false if the named file                 already exists.

  • public boolean exists() throws IOException
             This method returns the true if the file or directorypathnames are present.
  • public boolean delete()  
             Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory,               then the directory must be empty in order to be deleted. 
            This method returns the names of all files & sub-directories present in the specified directory.
  • public boolean mkdir()
Creates the directory named by this directory pathname.
  • public boolean isFile()


  • Filewriter object is used to write charcter data to the File.

Methods Present in FileWriter:

  • write(int ch)
           this method is used to write a single charcter to the File.
  • write(Char[] ch)
          This method is used to write an array of characters to the file.
  • write(String s)
         This  method is used to write a string to a file.
  • flush()
      this method is used to save the total character data to the file.


   This class is used to read the character data from the file.
     FileReader f = new FileReader(String name);
    FileReder f = new FileReader(file f);
Methods of FileReader:
  • int read()
          This method is used to read next character from the file and return its unicode value.
  • int read(Char[] ch)
          This method is used to read characters from the file into the char array.
  • close()
          This method is used to close the stream.

No comments:

Post a Comment