How to Open a File in Java
There are following ways to open a file in Java:
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.
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:
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.
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.
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. 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.
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 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.
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 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.
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:
Next TopicJava Tutorial
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week