Get Java to Find and Read a File
How to Open a File in Java
At that place are following ways to open a file in Java:
- Java Desktop form
- Coffee FileInputStream class
- Java BufferedReader form
- Java FileReader form
- Coffee Scanner class
- Java nio parcel
Coffee Desktop form
Coffee Desktop class provide an open up() method to open a file. Information technology belongs to a java.awt package. Desktop implementation is platform-dependent, so it is necessary to check the operating organisation supports Desktop or not. The Desktop class looks for an associated application 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 postal service client with an optional post-to URI.
- Launches the registered awarding to open, edit, or print a specified file.
The open() method of Desktop class launches the associated application to open up a file. Information technology 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 be.
- IOException: It is thrown when at that place is no application associated with the given file type.
- UnsupportedOperationExecution: If the current platform does not back up the Desktop.Activeness.Open up action.
Example
When we run the to a higher place plan, it opens the specified text file in the default text editor. We can likewise open the .docx, .pdf, and .jpg file.
Output:
Java FileInputStream course
Coffee FileInputStream class is used to open up and read a file. We can open and read a file by using the constructor of the FileInputStream course. The signature of the constructor is:
It accepts a file every bit an argument. It throws FileNotFoundException if the file does not exist or file proper noun is a directory.
Example
Output:
Coffee BufferedReader course
Java BufferedReader class reads text from a grapheme input stream. Information technology belongs to a java.io parcel. We use the constructor of the BufferedReader class to open up or read a file. The signature of the constructor is:
Information technology creates a buffering graphic symbol-input stream that uses a default?sized input buffer. Information technology uses a default size input buffer.
Example
Output:
Coffee FileReader grade
Coffee FileReader class is too used for opening and reading a file. It belongs to a java.io package. It is a convenience for reading characters of the files. Information technology 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 signature of the constructor is:
It accepts a file as an statement. Information technology throws the FileNotFoundException if the specified file does not exist or the file proper name is a directory.
Example
Output:
Java Scanner class
Java Scanner class is also used for opening and reading a file. The Scanner form belongs to java.util package. The constructor of Scanner class is used for opening and reading a file. The signature of the constructor is:
It accepts a file (to exist scanned) every bit an argument. Information technology too throws FileNotFoundException, if the source of the file is non found.
Example
Output:
Coffee nio parcel
readAllLines() method: The readAllLines() method is the method of File course. It reads all lines from a file and bytes from the file are decoded into characters using UTF-8 charset. Information technology 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 coffee.util package. It is used to obtain an empty list. The signature of the method is:
Case
Output:
Get Java to Find and Read a File
Source: https://www.javatpoint.com/how-to-open-a-file-in-java
0 Response to "Get Java to Find and Read a File"
Post a Comment