How to Open a File in Java?

Opening a file in Java is a fundamental operation that can be achieved through various classes and methods provided by the Java API, tailored to different file operations like reading or writing. For reading text files, the FileReader class combined with BufferedReader allows efficient reading line by line. For binary files, FileInputStream offers a way to read data byte by byte. Handling exceptions, particularly IOException, is crucial for robust file operation handling.

There are following ways to open a file in Java:

  • Java Desktop class
  • Java FileInputStream class
  • Java BufferedReader class
  • Java FileReader class
  • Java Scanner class
  • Java nio package

Java Desktop Class

Java Desktop class provide an open() method to open a file. It belongs to a java.awt package. Desktop implementation is platform-dependent, so it is necessary to check the operating system supports Desktop or not. The Desktop class looks for an associated application registered on the native desktop to handle a file. If there is no associated application or application is fails to be launched, it throws the FileNotFoundException. Launches the user default browser to show a specified URI.

  • Launches the user default mail client with an optional mail-to URI.
  • Launches the registered application to open, edit, or print a specified file.

The open() method of Desktop class launches the associated application to open a file. It takes a file as an argument. The signature of the method is:

The method throws the following exceptions:

  • NullPointerException: If the file is null.
  • IllegalArgumentException: It is thrown when the file does not exist.
  • IOException: It is thrown when there is no application associated with the given file type.
  • UnsupportedOperationExecution: If the current platform does not support the Desktop.Action.Open action.

Example

When we run the above program, it opens the specified text file in the default text editor. We can also open the .docx, .pdf, and .jpg file.

Output:

How to Open a File in Java

Java FileInputStream Class

Java FileInputStream class is used to open and read a file. We can open and read a file by using the constructor of the FileInputStream class. The signature of the constructor is:

It accepts a file as an argument. It throws FileNotFoundException if the file does not exist or file name is a directory.

A `SecurityException` occurs when there's an active security manager that restricts read access to the file, indicated by the `checkRead` method denying the operation.

Example

Output:

How to Open a File in Java

Java BufferedReader Class

Java BufferedReader class reads text from a character input stream. It belongs to a java.io package. We use the constructor of the BufferedReader class to open or read a file.

It wraps around other Reader instances, like FileReader, to buffer the input and improve performance by reducing the number of direct read operations from the underlying source. Ideal for reading large text files, BufferedReader enhances reading efficiency and simplifies the process of reading lines of text through its convenient readLine() method that returns a string containing the contents of a line, excluding any line-termination characters.

The signature of the constructor is:

It creates a buffering character-input stream that uses a default?sized input buffer. It uses a default size input buffer.

Example

Output:

How to Open a File in Java

Java FileReader Class

Java FileReader class is also used for opening and reading a file. It belongs to a java.io package. It is a convenience for reading characters of the files. It is used for reading raw bytes using the FileInputStream class. We use the constructor of the FileInputStream class to open and read a file.

The FileReader class in Java streamlines the process of reading text from files, offering a direct approach to access character data. Unlike byte-oriented streams that deal with raw data, FileReader focuses on characters, making it particularly useful for text file operations. The signature of the constructor is:

It accepts a file as an argument. It throws the FileNotFoundException if the specified file does not exist or the file name is a directory.

Example

Output:

How to Open a File in Java

Java Scanner Class

Java Scanner class is also used for opening and reading a file. The Scanner class belongs to java.util package. The constructor of Scanner class is used for opening and reading a file.

The Scanner class in Java is a tool that helps you to read and understand text from places like files or the user's input. It's really handy because it can recognize and turn text into different types of data, like numbers or words. The signature of the constructor is:

It accepts a file (to be scanned) as an argument. It also throws FileNotFoundException, if the source of the file is not found.

Example

Output:

How to Open a File in Java

Java nio package

readAllLines() method: The readAllLines() method is the method of File class. It reads all lines from a file and bytes from the file are decoded into characters using UTF-8 charset. It returns the lines from the file as a list. The signature of the method is:

Where path is the file path.

Above method is equivalent to invoking the following:

Collections.emptyList(): The emptyList() method is the method of Collection class which belong to java.util package. It is used to obtain an empty list. The signature of the method is:

Example

Output:

How to Open a File in Java
Next TopicJava Tutorial