YouTip LogoYouTip

Java Filereader

# Java FileReader Class [![Image 3: Java Stream](#) Java Stream](#) * * * The FileReader class is derived from the InputStreamReader class. This class reads data from a stream as characters. You can create the required object using one of the following constructors. Creates a new FileReader, given the File to read from. FileReader(File file) Creates a new FileReader, given the FileDescriptor to read from. FileReader(FileDescriptor fd) Creates a new FileReader, given the name of the file to read from. FileReader(String fileName) After successfully creating a FileReader object, you can refer to the methods in the following list to manipulate the file. | No. | File Description | | --- | --- | | 1 | **public int read() throws IOException** Reads a single character, returning an int variable representing the character read. | | 2 | **public int read(char [] c, int offset, int len)** Reads characters into the c array, returning the number of characters read. | ### Example ## Example import java.io.*; public class FileRead{public static void main(String args[])throws IOException{File file = new File("Hello1.txt"); // Create file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Write content to file writer.write("Thisn isn ann examplen"); writer.flush(); writer.close(); // Create FileReader object FileReader fr = new FileReader(file); char[]a = new char; fr.read(a); // Read array content for(char c : a)System.out.print(c); // Print characters one by one fr.close(); }} The compilation and execution of the above example produces the following result: Thisis an example * * Java Stream](#)
← Java FilewriterJava File β†’