Introduction to I/O

I/O is a very very huge topic. A general discussion of I/O includes topics such as

file I/O

console I/O

thread I/O

high-performance I/O

byte oriented I/O

character oriented I/O

I/O filtering and wrapping

serialization and a lot more 😱

The topics that included for OCA certification purpose are file I/O for characters and Serialization. And for a freshers mostly there will not be any questions on concepts other than this. I will fairly try to cover the most of the concepts and some concepts I would mark it as optional with the intention not to burden you with not so important knowledge, yet they are important !!,

Since you can always have access to my notes, you are welcome to comeback here anytime for your reference.

Topics name starts with ~ are optional for you but please do know this :

“There are no boundaries that one should set when it comes to learning, unless he or she clearly knows it’s worth setting the one”

– Vinay Noah


Command Line Arguments

video

Command Line Argument Real World Example1 :

video

Command Line Argument Real World Example2 :

video


Reading Input from Console

Reading Input from Console using java.util.Scanner API :

video

Reading Input from Console using java.io.Console API :

video


Introduction to Files

Lets list some of the important APIs present in I/O package.

File Class :

As per the JDK documentation File Class is “an abstract representation of file and directory pathnames.” Java developers cleverly divided the related behaviours to related classes.

In File class they kept the behaviours using which we can,

  • Check if file exists are not.
  • Check if we have read or write permission.
  • Is it a directory ?
  • Behaviours responsible for creating a new file or creating a new directory etc

Basically they kept all the methods we need to work around files and directories except Reading from file and Writing to file !!, for which the related behaviours they kept in FileReader and FileWriter and other classes.

Note: Here are mostly dealing with APIs that are dealing with character data. There are totally different APIs are present to deal with Stream of Data. That something I will cover later.

 

File Reader :

  • Behaviours responsible to reading characters in a file are present in this API.
  • It’s read() methods are fairly low-level and they allow you to read a single characters, whole stream of characters, or fixed number of characters.
  • FileReaders are usually wrapped by higher-level objects such as BufferedReader, which improves the performance and add more convenient way to read the content from file. In coming lessons I have wrote many examples so brace yourself till we reach there.

Buffered reader :

This class is used to make low-level Reader class 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 that data in a buffer.
  • When you ask for next character that line of data, it is retrieved from buffer, which minimizes the number of times the time-intensive, file-read operations are performed.
  • Along with that BufferedReader class provides a method called readLine() using which we can conveniently keep reading the nextLine of a file.

File Writer :

  • This class is used to write characters to file.
  • It’s write method allow you to write characters are strings to file.
  • Like FileReader, the FileWriters are usually wrapped by higher-level Writer object, such as BufferedWriter or PrintWriters, which provides better performance, and more flexible method to write data to a file.

 

Buffered Writer :

This class used to make lower-level classes like File Writer more efficient and easier to use.

  • Compared FileWriters, BufferedWriters write relatively large chunks of data to a file at once which minimizes the number of times that slow, file-writing operations are performed.
  • The BufferedWriter class also provides a method called newLine() which creates a platform-specific line separators automatically.

Print Writer :

  • This class was introduced in prehistoric times of Java but enhanced significantly in Java 5.
  • Because of some of the newly added methods and constructors, you might choose this over wrapping FileWriter wrapped in BufferedWriter.
  • The new methods such as  format(), printf() & append() make PrintWriters very flexible and powerful 💪.

Console :

This new Java 6 convenience class provides method to read input from console and write out to the console 😋.


Creating Files Using the File Class

video


FileReader and FileWriter

Writing text data to a file using FileWriter API :

video

Reading text data to a file using FileReader API :

video


BufferedReader and BufferedWriter

Writing text data to a file using BufferedWriter & FileWriter APIs:

video

Reading text data from a file using BufferedReader & FileReader APIs :

video

Bringing the above programs to our API Standards :

video


Working with binary data

video


Project Work with files & IO

Project Work with files & IO :


Serialization & Deserialization in Java :

video