Reading a text file in Java

Reading a text file in Java

Hi,

What are the options for Reading a text file in Java?

Share me some of the good examples.

Thanks

View Answers

November 27, 2024 at 12:20 PM

Hi,

Here is one example of reading a text file:

package net.roseindia;

import java.io.File;

import org.apache.commons.io.FileUtils;


/**
 * In this example we are going to 
 * read text file in Java
 * Web: https://www.roseindia.net
 * 
 */

public class ReadingTextFile {

    public static void main(String[] args) {
        String fileName = "data.txt";
        try {
            // Using apache-commons-io FileUtils
            String str = FileUtils.
                    readFileToString(new File(fileName), "utf-8");
            System.out.println(str);

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

    }

}

This code uses the apache-commons-io FileUtils class.

Thanks


November 27, 2024 at 12:23 PM

Hi,

Here is another example:

    // Example of reading a text file using FileInputStream,
    // BufferedReader and InputStreamReader classes
    FileInputStream fstream = new FileInputStream("data.tx");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

    String strLine;

    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console - do what you want to do
      System.out.println (strLine);
    }

    //Close the input stream
    fstream.close();

Thanks









Related Tutorials/Questions & Answers:
Reading a text file in java
in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks...Reading a text file in java  What is the code for Reading a text file
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
Advertisements
Reading Text file and storing in map
Reading Text file and storing in map  Hi I have multiple text files. I want to read thoses files and store those records in map. Map will be of "LinkedHashMap<String, Map<String, String>>" type. Please let me know
Reading big file in Java
Reading big file in Java  How to read a big text file in Java program?   Hi, Read the complete tutorial at How to read big file line by line in java? Thanks
Best way to reading file in java
Best way to reading file in java  Hi, As a beginner I want to learn about Java programming and make a program for reading a text file in java. What is the best way for reading file in Java? Thanks
Video Tutorial of reading file in Java
get the source code of reading a text file in Java. This source code can...Java Video Tutorial: Learn how you can read a big text file in Java? Java... Java example for Reading file into byte array Learn how
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java  ... n.getNodeName().equals("#text"); } public static void main(String[]args
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
Reading a file from Jar JAVA
Reading a file from Jar JAVA  I have added one excel sheet into a jar file. can anybody tell me how i can read that file. actually when i am running code from eclipse i able to read it but when i am adding that jar file
reading and displaying svg file through java
reading and displaying svg file through java  sample program for reading a svg file
Reading RDF file using Java code in Eclipse
Reading RDF file using Java code in Eclipse  Could you please tel me what this statement means - Model model = ModelFactory.createDefaultModel
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static...(); } m_dis.close(); There are 85,000 records in the text file all
initialise array by reading from file - Java Beginners
initialise array by reading from file  Hello, I wnat to know how i would initialise an array by reading a text file, which contains a simple pattern. for example the file may look as shown below, with the star character
Java error reading from file
Java error reading from file       Java error reading file are the common error occurred  in java that when the user encounter a corrupt file
Java example for Reading file into byte array
Java example for Reading file into byte array. You can then process the byte... in Java for reading file into byte array. Sometimes it becomes necessary... of reading file into byte array: Java file to byte array
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
Reading a big file effeciently
Reading a big file effeciently  How to read a large text file quickly without memory error in Java? What is the best method to read a big file very efficiently? Thanks   Hi, Kindly check the program Java Read File
External file reading in jsp
External file reading in jsp  i have written a jsp page(ReadExt.jsp) in my application which reads a text file content from a system... used java code in jsp page ... and tel me how can i make it work in other system
svg file reading and display
svg file reading and display  i want a sample program for reading and displaying the content of a svg file
Reading binary file into byte array in Java
Example code of reading binary file into byte array in Java This example shows you how to read a binary file into byte array from Java program. This type... is the complete code of the Java program that reads the binary file into byte array:ADS
External file reading in jsp
External file reading in jsp  i have written a jsp page(ReadExt.jsp) in my application which reads a text file content from a system...; The given code reads the given file. It uses BufferedReader class to read
Thread for reading txt file
Thread for reading txt file  how to use 3 thread to read 3 txt file? To create three threads for reading the file and three threads for getting the strings out of the queue and printing them. thanks
Line by Line reading from a file using Scanner Class
In this section, you will get to know about reading line by line a text file using java.util.Scanner class
Reading file into bytearrayoutputstream
input stream and byte output stream. This is and good example of reading file... In this example we have used the class InputStream for reading the file... import java.io.*; /** * Example of Reading file into byte array
Writing and Reading A File
and reading data from file for our assignment, but just stuck on how to proceed... into the text file. import java.io.*; import java.util.*; import javax.swing.*; import...;To extract the data from text file and save it to jtable, visit the following links
save text file - Java Beginners
save text file  hi i have just start programming in java.please guide me if i want to read a text file in java.then the text file is save in which directory
Problem reading word file
Problem reading word file  Deepak you provide me code for extarcting equation from a word file and also to write in a word file.But when I again want to read preveously created word file(created by your code) it gives an error
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The file contains nothing but strings
Reading .doc file using swing
Reading .doc file using swing  Sir, Could U Please Tell me the Way To Read .doc file in java using swing,with code
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
Reading files in Java
of finance that reads the text file and process the data in it. How to read the text file in Java. What is the best way to read the file in Java as its big file. Please provide me the example code for reading big text file in Java. What do you
file reading - Swing AWT
file reading  hi everybody, i want to know how to read contents of doc or exe file except for txt file in java.   Hi Friend, Use Jakarta POI library to read the contets of doc file: Here is the code: import
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 a simple program for reading text file and writing data into another text file... and we want to copy the content into another text file from our Java program
Err while reading CSV file using POI - Java Beginners
Err while reading CSV file using POI  Hi, When i am trying to read a CSV file using POI Dile system, the below error is coming up... know, why this is coming up. Is there any other way to read the CSV file. Thanks
How to append text to an existing file in Java
How to append text to an existing file in Java  How to append text to an existing file in Java
Reading a xml file - JSP-Servlet
Reading a xml file  Thanks for ur answer sir but problem is that i have to do a reading a xml file of a employee record and then i have to use a employee details to send mail to those employees how to do i sir please help me
How to read file in java
How to read file in java Java provides IO package to perform reading... to read file in Java?" Another way of reading a file: The another program  use DataInputStream for reading text file line by line
take variables in text file and run in java
take variables in text file and run in java  I have a text file which have variables 17 10 23 39 13 33 How to take "17"in java code
problem while reading .csv file
problem while reading .csv file  I have a problem here..... i am reading a .csv extention file which has some value....but the problem is der is an amount column which contains , in between (eg) 3,899.00 which inturns creates
Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml
Reading and querying an Excel file in JavaScript
Reading and querying an Excel file in JavaScript  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new
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
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
Reading string from file timed
Reading string from file timed  So I want to make a file reader/ buffered reader that reads a new line of the textfile, lets say every 30 second. Like it reads the first line, waiting 30 seconds, read the next line and so one
how to read text file with java 8 stream api
, Following example is for reading text file line by line in Java using the stream api...how to read text file with java 8 stream api  Hi, I want to use Java 8 Stream API for reading text file line by line. I am trying to find example
searching a text string in file - Java Beginners
searching a text string in file  code to match a string in a file that is already mentioned in java code.if the string matches any part of a string that whole string is printed
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... Instruction of Reading the file line by line in Java: ADS_TO_REPLACE_2 You
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
how to convert text file to xml file in java. - XML
how to convert text file to xml file in java.  Hi all, I m having some problem. Problem is I want to convert a text file which is having the no of record(i.e no of different line of information)to a xml file through java

Ads