SCJP – Java I/O

Some diagrams and tables in this post from SCJP Sun Certified Programmer for Java 6 book.


File The API says that the class File is "An abstract representation of file and directory pathnames." The File class isn't used to actually read or write data; it's used to work at a higher level, making new empty files, searching for files, deleting files, making directories, and working with paths.
FileReader This class is used to read character files. Its read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performanceand provide more convenient ways to work with the data.
BufferedReader This class is used to make lower-level Reader classes like FileReader more efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once, and keep this data in a buffer. When you ask for the next character or line of data,it is retrieved from the buffer, which minimizes the number of times tha ttime-intensive, file read operations are performed. In addition, BufferedReader provides more convenient methods such as readLine(), thatallow you to get the next line of characters from a file.
FileInputStream A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
ObjectInputStream An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
FileWriter This class is used to write to character files. Its write()methods allow you to write character(s) or Strings to a file. FileWriters areusually wrapped by higher-level Writer objects such as BufferedWriters orPrintWriters, which provide better performance and higher-level, moreflexible methods to write data.
BufferedWriter This class is used to make lower-level classes likeFileWriters more efficient and easier to use. Compared to FileWriters,BufferedWriters write relatively large chunks of data to a file at once,minimizing the number of times that slow, file writing operations are performed. The BufferedWriter class also provides a newLine()method to create platform-specific line separators automatically.
PrintWriter This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter witha File or a String), you might find that you can use PrintWriter in placeswhere you previously needed a Writer to be wrapped with a FileWriter and/ora BufferedWriter. New methods like format(), printf(), and append()make PrintWriters very flexible and powerful.
Console This new, Java 6 convenience class provides methods to read input from the console and write formatted output to the console.
FileOutputStream A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
ObjectOutputStream An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.


Don't forget to study the io objects constructors:


  • dir.list()  //returns a new String[] with a list of all file and folders in the dir directory;
  • new PrintWriter("myFile.txt");
    • When the constructor is called, it truncates the existing file to zero size.
  • File f1 = new File("sub1");;     f1.mkdir();
    • if the file exists doesn't throw a exception. It returns a boolean indicating whether or not a new
      directory was made
    • Most of interfaces of returns boolean to indicate error or success.
  • File f3 = new File(f1, "sub3");;   PrintWriter pw = new PrintWriter(f3);
    • Creates a empty file, on the f1 folder, but only during the PrintWriter constructor.
  • f.renameTo() method takes only anothe File as arg, not a string.


One Response to “SCJP – Java I/O”

  1. Ana Rebelo Says:

    Here I am at your page again, from a google search about and SCJP 😀
    Good work!

Leave a Reply