Read Whole Text File Using FileUtils in Java

Read Whole Text File Using FileUtils in Java

HI Guys,

Is there any code for reading whole text file into string in Java? I checked somewhere and its says it can be loaded using FileUtils in Java.

Help me by sharing the code.

Thanks

View Answers

November 27, 2024 at 10:50 AM

Hi,

You can use FileUtils class from commons-io to read whole text file into a string.

First all add add following dependency in your pom.xml file if you are using maven:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.18.0</version>
</dependency>

Maven will download required jar files in your project. You can use:

String str = FileUtils.
          readFileToString(new File(fileName), "utf-8");

to read file into string. Here is full code:

package net.roseindia;

import java.io.File;

import org.apache.commons.io.FileUtils;

public class ReadWholeTextFileUsingFileUtils {

    public static void main(String[] args) {
        String fileName = "data.txt";
        try {
            String str = FileUtils.
                    readFileToString(new File(fileName), "utf-8");
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Thanks









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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
Read text File  Hi,How can I get line and keep in a String in Java
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  
Java read text file
text file in Java?": ADS_TO_REPLACE_3 Example of Read text File Line... a text file one line at a time. It can also be used to read large text files... to Read file line by line is by using Scanner Class. The benefit of using Scanner
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access... me the things where i went wrong java servlet program protected void... ServletException, IOException { response.setContentType("text/xml"); FileRead fr
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... and we want to copy the content into another text file from our Java program
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 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
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... you kind advice and let's know how to read a large text file line by line in java
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
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
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
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
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...(in)); readline() is used to read the next line. Example of Java Read File:ADS_TO_REPLACE_3
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
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
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
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 file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... problem is to find the best way to read the file in Java. I just searched the google... to Read a file line by line using BufferedReader, efficiently and using less memory
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 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, This is example code: package my.code; import java.io.BufferedReader; import
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
avoid using the whole URL
avoid using the whole URL  How can I avoid using the whole URL
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.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
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
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
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
Java Read File Line by Line - Java Tutorial
Java Read File Line by Line - Example code of reading the text file... how to read a text file in Java one line at a time: Video Tutorial... to reading file in Java How to Read Excel file Using Java
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
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 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
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text... to read file in Java?" Another way of reading a file: The another
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
Java read binary file
Java read binary file  I want Java read binary file example code... at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know that there are many ways to read using different API like SAX, DOM , XOM Jar...Read XML using Java  Hi All, Good Morning, I have been working
read xml using java
read xml using java  <p>to read multiple attributes..."\interface type=""\level="0"\source_file="ppp" etc as wise each attribute n... implements the whole functionality of the electrical parking brake (ECR, ERR, DAR
Read File from specified path in Java
: You can also read the file using the Scanner class of Java. Here is the code...How to read a file from a specified path in Java? After learning so many... reads a text file from specified path in Java. We will give complete path
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
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
Java read lines from file
to read the whole file in one go. So, in my case reading file line by line is only...Java read lines from file  Any code example related to Java read... of reading file line by line in Java. Can any one share me the code for reading
Java file read write operation
Java file read write operation  how to read and write the data from text file.Suppose i have text file with 4 fields name ,roll no ,marks1,marks2..."); oos.writeInt(8000); oos.writeObject("Ankit"); oos.writeObject("Java
Video Tutorial of reading file in Java
Java Video Tutorial: Learn how you can read a big text file in Java? Java... file in Java How to Read Excel file Using Java Read Specific Line from file Using Java How to read file

Ads