FileReader(String): This method is available in the java.io.FileReader class of Java.
Syntax:
java.io.FileReader.FileReader(String fileName) throws FileNotFoundException
This method takes one argument. This method creates a new FileReader, given the name of the file to read, using the platform's default charset.
Parameters: One parameter is required for this method.
fileName: the name of the file to read.
Throws:
FileNotFoundException - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
Approach 1: When no exception
Java
import java.io.FileNotFoundException;import java.io.FileReader;public class FileReader3 {public static void main(String[] args)throws FileNotFoundException {String fileName = "D:\\hello.txt";FileReader fileReader = new FileReader(fileName);System.out.println(fileReader);}}
Output:
java.io.FileReader@7637f22
Approach 2: FileNotFoundException
Java
import java.io.FileNotFoundException;import java.io.FileReader;public class FileReader3 {public static void main(String[] args)throws FileNotFoundException {String fileName = "D:\\hello2.txt";FileReader fileReader = new FileReader(fileName);System.out.println(fileReader);}}
Output:
Exception in thread "main" java.io.FileNotFoundException: D:\hello2.txt (Access is denied) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:211) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108) at java.base/java.io.FileReader.<init>(FileReader.java:60)
Some other methods of FileReader
FileReader(File): This method creates a new FileReader, given the File to read, using the platform's default charset.
FileReader(FileDescriptor): This method creates a new FileReader, given the FileDescriptor to read, using the platform's default charset.
FileReader(String): This method creates a new FileReader, given the name of the file to read, using the platform's default charset.
FileReader(File, Charset): This method creates a new FileReader, given the File to read and the charset.
FileReader(String, Charset): This method creates a new FileReader, given the name of the file to read and the charset.
No comments:
Post a Comment