Using read( ) method of FileReader I/O Class, we can read the characters from the provided text file.
read( ) method reads one character at a time from text file and returns the integer value based on the read character. We have to use the (char) casting to convert the integer value returned by the read( ) method into a character. And also as the read( ) method only reads one character at a time, In order to read all the characters in the text file, we have to use the read( ) method in an iterative loop like for loop. And in order to find the length (i.e. number of characters) in the text file we have to use length( ) method of File Class. Please find the below example to understand -
Example -
//Assuming xyz.txt is available in our project workspace and has the text "Hi ! How are you?"
File file1 = new File("xyz.txt"); //Creating File Class object file1 for xyz.txt file in our project workspace
FileReader fr = new FileReader(file1); //Creating FileReader Class object fr for file1
long length = file1.length( ); //Finding the number of characters in the xyz.txt file
//Create a for loop to execute the read( ) method until the characters in the text file are completed
for(long i=0; i<length; i++)
System.out.print((char) fr.read( )); //Casting to characters and printing on the same line
Output -
Hi ! How are you?
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE, create a new Java Project 'Project 51' as shown below -
2. Create a new Java Class 'FileReaderDemo.java' with main( ) method as shown below -
3. Go to Project Workspace as shown below -
4. Create a text file say 'xyz.txt' as shown below -
5. Open the created text file, enter text "Hi ! How are you?" and save & close the file as shown below -
6. In Eclipse IDE -> 'FileReaderDemo.java', create a File Class object for the created text file 'xyz.txt' as shown below (Resolve any import errors) -
7. Create FileReader Class object to read the text from the xyz.txt file using read( ) method by passing the above created file1 File Class object as shown below (Resolve any import errors) -
8. View the error and select 'Add throws declarations' option from the error message to resolve the error as shown below (I am adding throws for explanation purpose, in reality we should add try/catch to resolve the error) -
9. Observe that FileNotFoundException is added to the method declaration and the error got resolved as shown below -
10. Now find the length of the 'xyz.txt' file (i.e. The number of characters in the file) using length( ) method of File Class and assign the result to the long data type variable as the length( ) method returns long value as shown below -
11. Now create a for loop to iterate the number of character times as shown below -
12. Type Cast and print the integer value returned by the read( ) method of FileReader Class to char by writing the print statement inside the for loop as shown below (As a result read( ) method can read the characters one by one as the loop get incremented). Use print( ) instead of println( ) here -
13. View the error and select 'Add throws declaration' option from the error message as shown in the below screenshot (I am adding throws for explanation purpose, in reality we should add try/catch to resolve the error) -
14. Observe that the IOException got added to the method declaration and the error got resolved as shown below (As IOException is the parent class of FileNotFoundException Class, IOException class in the method declaration has replaced the earlier FileNotFoundException) -
15. View the warning message as shown below -
16. Close the object fr using the close( ) method and observe that the warning got resolved as shown below -
17. Save & Run the 'FileReaderDemo.java' and observe that the text inside the 'xyz.txt' file in our workspace path is read by the read( ) method by getting executed in the for loop, integer values returned by the read( ) method got type casted to characters and the print( ) statement has printed all characters in the same line as shown in the below output -
Hence we have used read( ) method of FileReader Class to read all the characters from a text file in our project workspace.
Please comment below to feedback or ask questions.
2 comments:
Hi Arun,
I dont see any of ur post after 164 tutorial related to selenium webdriver could u plz guide me about that
@arpit - Check the contents page.
Post a Comment