All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Java Read Text File

Last Updated : Mar 11, 2024

Java Read Text File

In this article we will show you the solution of java read text file, to achieve desired result everyone need to import io package, which helps you to read file and display that content to users.

In case you using eclipse then each program adds default package or basic package itself at the first line.

Otherwise you will get error and with the help of file object and file reader object you can read text file easily.

Step By Step Guide On Java Read Text File :-

We named main class as ‘Rdfile_prgm’, there we provide definition for public static main method.

Then we need to create object for desired file through passing file path as argument.

To read files we file reader object, so we defined ‘BufferedReader’ object ‘ob’ with parameter of file object.

Because we need to read a particular file and created string variable ‘s’ to store the had read content and displayed line by line until reach the null character with the help of loop.

//Read Text File
package java_prgms;
class Rdfile_prgm
 public static void main(String args[])throws Exception
  File f=new File("C:\\Users\\welcome\\Desktop\\ff.txt");
  BufferedReader ob=new BufferedReader(new FileReader(f));
  String s;
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First displaying package will inserted by eclipse to support java codes, if you try codes in eclipse platform each time it will imported after successful creation of java project.
  3. In main class ‘Rdfile_prgm’, ensure whether you are given name for class and saved program name same or not. If same you do not get confuse at any case during execution otherwise name for execution same for both run and compiling comment.
  4. Within main class you might specify main method as foremost step. To read a text file first we defined File object ‘f’ with desired file path and our file name is ‘ff.txt’. The path may vary for you depends on the your text file presented location.
  5. BufferedReader object ‘ob’ created with File object ‘f’ to convey which file going to be read and empty string variable ‘s’ created to assess the iteration and store file text on a variable.
  6. ‘readLine()’ is plays vital to in this program to read the lines in the file by appending this method with reader object and condition set it as until meets null value it reads line and storing on s variable then side by side printed by println().
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to read a text file in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Rdfile_prgm‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output of had read content from your desired text file ‘ff.txt’.

I hope this article on java read text file helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪