Java - Things to know before your OCJP exam - File IO

1. File Objects are not used to read or write data. The possible operations that can be performed with File is to make empty new files / directories, searching for files and directories, deleting files and directories and working with paths.

2. File file = new File("xyz.txt"); doesn't create the file in the file system. Either of the following code will help create the file.

    File file = new File("xyz.txt");
    boolean b = file.createNewFile(); // this creates the file


    File file = new File("xyz.txt");
    FileWriter fw = new FileWriter(file); // this creates the file

3. FileReader and FileWriter are usually wrapped by BufferedReader, BufferedWriter and PrintWriter for performance and convenience.

4. BufferedWriter provides a newline() method to create platform specific line separator automatically.

5. After Java 1.5, PrintWriter can be build using File and String too along with OutputStream and FileWriter.

6. flush() is used to flush the residual characters in the buffer to the file. It should be the statement before close.

7. Following are the constructors and methods available with File IO classes


Constructors - (File, String) , (String), (String,String )
Methods - createNewFile , delete, exists, isDirectory, isFile , list , mkdir, renameTo


Constructors - (File), (String)
Methods - close, flush, write


Constructors - (Writer)
Methods - close, flush, newline, write


Constructors - (File), (String), (OutputStream),(Writer)
Methods - close, flush, format, printf, print, println, write


Constructors - (File) , (String)
Methods - read


Constructors - (Reader)
Methods - read, readLine

8. file.createNewFile doesn't create the file and returns false if the file with the same name is already there.

9. file.mkdir() is used to create a new directory.

10. File constructor (File,String) can be used to create a file in the sub directory alternatively instead of specifying the complete path as String using (String) constructor.

11.  When there is no more data to read using, deadline returns null as a mark of read completion.

12. We cannot delete a directory using (File)dir.delete() if its not empty though we can always rename it using dir.renameTo(). To rename it we should specify correct file handle for the new Name as it being null will result in NullPointerException.

13. We can search for files and directories by using (File)dir. list() method and get the list of files and sub directories and then we can loop through the list to find the match.  


Following are valid declaration and initialization -

File file = new File("xyz.txt");

File file = new File("abcDir","xyz.txt");

File file = new File(new File("abcDir"),"xyz.txt");

FileWriter fileWriter = new FileWriter(new File("xyz.txt"));

FileWriter fileWriter = new FileWriter("xyz.txt");

BufferedWriter buffWriter = new BufferedWriter(new FileWriter(new File("xyz.txt"));

BufferedWriter buffWriter = new BufferedWriter(new PrintWriter("xyz.txt"));

BufferedWriter buffWriter = new BufferedWriter(new PrintWriter(new File("xyz.txt")));

BufferedWriter buffWriter = new BufferedWriter(new PrintWriter(new FileWriter(new File("xyz.txt")))); 

PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("xyz.txt"))));

PrintWriter printWriter = new PrintWriter(new BufferedWriter(new File("xyz.txt")));

PrintWriter printWriter = new PrintWriter(new File("xyz.txt"));

PrintWriter printWriter = new PrintWriter("xyz.txt");

Following are illegal initializations

File file = new File(xyz.txt);

File file = new File("xyz.txt",new File("abcDir"));

FileWriter fileWriter = new FileWriter(new PrintWriter("xyz.txt"));

FileWriter fileWriter = new FileWriter(new BufferedWriter("xyz.txt"));

BufferedWriter buffWriter = new BufferedWriter("xyz.txt"));

BufferedWriter buffWriter = new BufferedWriter(new File("xyz.txt")));

PrintWriter printWriter = new PrintWriter(new File(new FileWriter("xyz.txt")));