
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Basic Datatypes
- Java - Basic Operators
- Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
- Object Oriented Programming
- Java - Object & Classes
- Java - Methods
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Polymorphism
- Java - Overriding
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner classes
- Java Data Types
- Java - Characters
- Java File Handling
- Java - Files and I/O
- Java Error & Exceptions
- Java - Exceptions
- Java Multithreading
- Java - Multithreading
- Java Synchronization
- Java - Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Thread Control
- Java Networking
- Java - Networking
- Java - URL Processing
- Java - Generics
- Java Collections
- Java - Collections
- Java List Interface
- Java - List Interface
- Java Queue Interface
- Java - Queue Interface
- Java Map Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java Set Interface
- Java - Set Interface
- Java - SortedSet Interface
- Java Data Structures
- Java - Data Structures
- Java - Enumeration
- Java Collections Algorithms
- Java - Collections
- Java - Iterators
- Java - Comparators
- Java Miscellenous
- Java - Regular Expressions
- Java - Serialization
- Java - Sending Email
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java - FileReader Class
This class inherits from the InputStreamReader class. FileReader is used for reading streams of characters.
This class has several constructors to create required objects. Following is the list of constructors provided by the FileReader class.
Sr.No. | Constructor & Description |
---|---|
1 | FileReader(File file) This constructor creates a new FileReader, given the File to read from. |
2 | FileReader(FileDescriptor fd) This constructor creates a new FileReader, given the FileDescriptor to read from. |
3 | FileReader(String fileName) This constructor creates a new FileReader, given the name of the file to read from. |
Once you have FileReader object in hand then there is a list of helper methods which can be used to manipulate the files.
Sr.No. | Method & Description |
---|---|
1 | public int read() throws IOException Reads a single character. Returns an int, which represents the character read. |
2 | public int read(char [] c, int offset, int len) Reads characters into an array. Returns the number of characters read. |
Example
Following is an example to demonstrate class −
import java.io.*; public class FileRead { public static void main(String args[])throws IOException { File file = new File("Hello1.txt"); // creates the file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); // Writes the content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close(); // Creates a FileReader Object FileReader fr = new FileReader(file); char [] a = new char[50]; fr.read(a); // reads the content to the array for(char c : a) System.out.print(c); // prints the characters one by one fr.close(); } }
This will produce the following result −
Output
This is an example