Java FileReader Class with Examples
Using Files or Blob objects to specify the file or data you want to read, the FileReader object allows Web applications to read the contents of files or raw data buffers stored by a user’s computer.
Here are some key points about FileReader in Java
- Reading character data
- Efficient text reading
- Use with text files
try (FileReader reader = new FileReader("sample.txt")) { int character; while ((character = reader.read()) != -1) { System.out.print((char) character); } } catch (IOException e) { e.printStackTrace(); }
Benefits:
- Character-Oriented
- Efficient for Text Files
- Exception HandlingC
- Convenient for Line-Based Reading
The FileReader property allows web applications, using Files or Blob Objects to specify a file or data that they will open in an asynchronous manner, to access contents of files and Raw Data buffers from the user’s computer.
Example
import java.io.*; class TechVidvan{ public static void main(String[] args) { try { FileReader fileReader= new FileReader("output.txt"); System.out.println("Text in tutorial: \n"); int i; while ((i = fileReader.read()) != -1) { System.out.print((char)i); } } catch (Exception e) { System.out.println(e); } } }
Output:
Text in tutorial:
welcome to TechVidvan Java Course
FileReader:
- The FileList object returned after a user selects files using the input> element or drag and drop operation’s DataTransfer property can be used to retrieve file objects.
- Only files that have been expressly chosen by the user can be accessed with FileReader, whether you use an HTML input type=“file”> element or drag and drop.
- From the user’s file system, it is not possible to read a file by its pathname. Use the File System Access API to get files by pathname on your client’s file system.
- Use the standard Ajax solution with CORS permission to read files on a server if you wish to read cross domains.
Java IO FileReader Class:
A Java FileReader class that can read a stream of characters out of files is available in the java.io package.
The Java IO FileReading class uses either the requested charset or the default keyboard format on your platform to read from bytes to characters.
- Charset: For the purpose of defining methods to make Encoders and Decoders, as well as for retrieving several names in combination with Charsets, a class called Charset is employed.
- The default Charset is set as follows: During an implicit computer startup, the default charset is defined, depending on the location and charset of the underlying operating system.
First, here’s a Blob-like constructor
new File(fileParts, fileName, [options])
fileParts – is an array of Blob/BufferSource/String values.
fileName – file name string.
options – optional object:
lastModified – timestamp (integer) of the last modification.
Conclusion
Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java. You can view the contents of a file’s characters by character when you create an object called FileReader and use your Read() method. Remember to handle potential exceptions by wrapping the code in a try-catch block and closing the file reader object after reading the file.