Read text file using FileReader

Read text file using FileReader

Hi,

I want to know how to read one character at a time from text file in Java using FileReader Class.

I am learning Java file handling.

Help me in easy to learn example.

Thanks

View Answers

November 27, 2024 at 5:23 AM

Hi,

The FileReader class is packed in the java.io package of Java. This class can be used to read data one character at a time from a text file. So, we are going to use the FileReader class for our purpose. It is noted that this class extends the InputSreamReader class. Here is the complete code:

package net.roseindia;

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

/**
 * In this example we are going to 
 * File Reader class to read a text file
 * Web: https://www.roseindia.net
 * 
 */

public class ReadTextFileUsingFileReader {

    public static void main(String[] args) {
        String fileName = "data.txt";
        try {
            FileReader fr=new FileReader(fileName);
            int i;
            // Read one character at a time and print
            // On the console
            while((i=fr.read())!=-1){
                System.out.print((char) i);
            }

            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Thanks









Related Tutorials/Questions & Answers:
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
how to read file line by line using filereader in java
how to read file line by line using filereader in java  Hi, how to read file line by line using filereader in java? Thanks   Hi..."; try (BufferedReader br = new BufferedReader(new FileReader(fileName
Advertisements
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
Java read file
There are many ways to read a file in Java. DataInputStream class is used to read text File line by line. BufferedReader is also used to read a file in Java when the file in huge. It is wrapped around FileReader. Java supports reading
Read Lines from text file
Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each... read from the text file and displays the output as desired. Unable to read the rest
Steps to read text file in pyspark
Steps to read text file in pyspark  Hi, I am learning to write program in PySpark. I want to simply read a text file in Pyspark and then try some code. What are the Steps to read text file in pyspark? How much time it takes
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  ... Exception { super("MegaViewer"); FileReader f=new FileReader..."); FileReader f=new FileReader("A.java"); BufferedReader brk=new
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1 to 10 from the text file. If the loop reached fifth line, the br.readLine() method
How to read text file in Servlets
. Read the file line by line using the while loop  ((text = reader.readLine... How to read text file in Servlets  ... file in servlets. In this example we will use the input stream to read the text
Read text file in PySpark
Read text file in PySpark - How to read a text file in PySpark? The PySpark...().setAppName("read text file in pyspark") sc = SparkContext(conf=conf... configuration conf = SparkConf().setAppName("read text file in pyspark") sc
how to read a text file with scanner in java
how to read a text file with scanner in java  Hi, I am looking for the example code in Java for reading text file line by line using the Scanner class. how to read a text file with scanner in java? Thanks   Hi
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3 columns contigID length size and then display sequence like "ATGCGSA..." in text
read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file
Java read text file
a text file one line at a time. It can also be used to read large text files... text file in Java?": ADS_TO_REPLACE_3 Example of Read text File Line... to Read file line by line is by using Scanner Class. The benefit of using Scanner
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating.... Our requirement is to read a text file and then write the content of the text...: In this tutorial we have learned to read a text file and then write it to another file
how to read file using InputStreamReader in java
how to read file using InputStreamReader in java  Hi, I want to learn to use the InputStreamReader class of Java and trying to read a text file with the class. how to read file using InputStreamReader in java? Thanks  
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... to Read a file line by line using BufferedReader, efficiently and using less memory... to me in my company. Here I have many big text files. I have to read these files
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access... ServletException, IOException { response.setContentType("text/xml"); FileRead fr...-fold> } jsp: <%@page contentType="text/html"%> <%@page pageEncoding
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text File  I need to read a file that was selected by the user using... to another text file. When that is done the output values of that file need
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion
read from file and store using hash map
read from file and store using hash map  I was stuck with a java project where I have to read a file with two different concepts and store them differently in hashmap. My data file would be something like Adults: Name, xyz
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been assigned a work to read big text file and extract the data and save into database... memory assigned is also a limit. So, we have decided to read the text file line
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
how to read text file with java 8 stream api
how to read text file with java 8 stream api  Hi, I want to use Java... code. how to read text file with java 8 stream api? Thanks   Hi, Following example is for reading text file line by line in Java using the stream api
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
Read text file to 2D array and sorting the second column
Read text file to 2D array and sorting the second column  we found that the student names with marks in a text file the marks decreases significantly... their names in a text file
How to read and compare content of two different text file
Description: In the given example you will see how a two text file's content are compared. The BufferedReader class allow us to read a file. The readLine() method used to read the contents of the specified file.  The way
FileReader
FileReader  How to read a file in java? My file like this Name,Age, sex i read the file and i want to insert the data in the Name obj and age obj and sex obj how
Java Read .doc file using POI library
Java Read .doc file using POI library In this section, you will learn how to read the word document file using POI library. The class HWPFDocument throw all of the Word file data and the class WordExtractor extract the text from
program to create student report using java applet,read the input using text boxesand generate the grades..?
program to create student report using java applet,read the input using text boxesand generate the grades..?   sir plz give me java applet codes for above question
write a progam for bubble sort using file read nd write?
write a progam for bubble sort using file read nd write?  hi, please give the code
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database... upload excel file and update database using JSP ? Thanks in Advance
Java Read File Line by Line - Java Tutorial
Java Read File Line by Line - Example code of reading the text file... class to Read text File Line by Line. Our example program is reading a text... be used to read large text files also. Since this program is using
read a file
read a file  read a file byte by byte   import java.io.File..._TO_REPLACE_1 public static void main(String[] args) { File file = new File("D://Try.txt"); try { FileInputStream fin = new FileInputStream(file
file read
file read  hi i am reaing from a file which has punjabi words. can some one help with me some code
Convert pdf to text file using Java
How to Convert pdf to text file in Java In this section, you will learn how to convert pdf file to text file in Java Programming. We have used itext api for this purpose. To read resume.pdf  file, we have used PDFReader class
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
Search word from text file using Java
How to Search word from text file using Java In this section, we are going to search a word from the text file. For this, we have created a swing button... the user typed his/her name, it will check the name in the text file. To check
create login page using data from text file
create login page using data from text file  I want to create login page using data store in textfile(data submit from regiter page to textfile) using jsp and servlet. Thanks
Problem while using a HashMap for writing text ina RTF file. - Framework
Problem while using a HashMap for writing text ina RTF file.  Hi, I am trying to generate a RTF file using iText.jar, wherein I am taking... matched to the string then only write to the RTF file. Problem is Document writes
I am creating one jsp page in which I read in a text file, then display that data in tabular format. Now I need to calculate a total.
I am creating one jsp page in which I read in a text file, then display... the file using BufferedReader, then writing the output like this: String... FileReader(fileName)); int lineCount=1; String fileData = ""; out.println("<
How to Read Excel file Using Java
How to Read Excel file In this section,you will learn how to read excel file... and every cell and stored the excel file values into the vector.This Vector data is then used to display file values on the console. Here is the code
Read the file Contents
Read the file Contents  Ravi Raj,Vijay45Shankar,234 Guna,345,Meet me,654,Cow Read file contents and Print the no.of words and Numbers./p> Thanks, Dinesh Ram   The given code read the file content and print
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import

Ads