How to Read Files From Desktop in Java

How to Open a File in Java

There are post-obit ways to open up a file in Java:

  • Coffee Desktop class
  • Coffee FileInputStream grade
  • Coffee BufferedReader class
  • Java FileReader class
  • Java Scanner form
  • Coffee nio package

Java Desktop class

Coffee Desktop class provide an open() method to open a file. It belongs to a java.awt parcel. Desktop implementation is platform-dependent, so it is necessary to bank check the operating organisation supports Desktop or non. The Desktop course looks for an associated awarding registered on the native desktop to handle a file. If at that place 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 up() method of Desktop class launches the associated application to open a file. It takes a file as an statement. The signature of the method is:

The method throws the post-obit 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 back up the Desktop.Action.Open action.

Example

When we run the above program, information technology 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 form

Java FileInputStream grade is used to open up 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 be or file name is a directory.

Example

Output:

How to Open a File in Java

Java BufferedReader course

Java BufferedReader class reads text from a graphic symbol input stream. It belongs to a coffee.io parcel. We use the constructor of the BufferedReader course to open or read a file. The signature of the constructor is:

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

Example

Output:

How to Open a File in Java

Java FileReader class

Coffee FileReader class is also used for opening and reading a file. Information technology belongs to a java.io packet. Information technology is a convenience for reading characters of the files. It is used for reading raw bytes using the FileInputStream class. Nosotros utilise the constructor of the FileInputStream class to open up and read a file. 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 proper noun is a directory.

Example

Output:

How to Open a File in Java

Java Scanner class

Java Scanner class is too used for opening and reading a file. The Scanner class belongs to java.util bundle. The constructor of Scanner class is used for opening and reading a file. 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 non found.

Instance

Output:

How to Open a File in Java

Java nio parcel

readAllLines() method: The readAllLines() method is the method of File class. Information technology reads all lines from a file and bytes from the file are decoded into characters using UTF-eight charset. It returns the lines from the file equally a listing. 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 Drove form which vest 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

roachprisomany.blogspot.com

Source: https://www.javatpoint.com/how-to-open-a-file-in-java

0 Response to "How to Read Files From Desktop in Java"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel