Read-Write Text File In Java - Tutorials For WebDriver

We have learnt about String class and Its different functions In my previous post. Now our next topic Is How to write In to text file or How to read text file In java software development language. Many times you will need reading or writing text file In your selenium webdriver software automation test case development. For Example, You are reading some large data from web page of software web application and wants to store It In text file to use It somewhere else In future. Same way, You have to read data from file for some purpose.

It Is very easy to Create, Write and read text file In java software development language. We can use java built in class File to create new file, FileWriter and BufferedWriter class to write In to file, FileReader and BufferedReader class to read text file. 

Bellow given example will first of all create temp.txt file In D: drive and then write two line In to It. Then It will read both lines one by one from text file using while loop and print In console. You can use bellow given example code of software development language for reading or writing text file In your selenium webdriver software automation test case whenever needed.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class RW_File {

 public static void main(String[] args) throws IOException {
  //Create File In D: Driver.  
  String TestFile = "D:\\temp.txt";
  File FC = new File(TestFile);//Created object of java File class.
  FC.createNewFile();//Create file.
  
  //Writing In to file.
  //Create Object of java FileWriter and BufferedWriter class.
  FileWriter FW = new FileWriter(TestFile);
  BufferedWriter BW = new BufferedWriter(FW);
  BW.write("This Is First Line."); //Writing In To File.
  BW.newLine();//To write next string on new line.
  BW.write("This Is Second Line."); //Writing In To File.
  BW.close();
  
  //Reading from file.
  //Create Object of java FileReader and BufferedReader class.
  FileReader FR = new FileReader(TestFile);
  BufferedReader BR = new BufferedReader(FR);
  String Content = "";
  
  //Loop to read all lines one by one from file and print It.
  while((Content = BR.readLine())!= null){
   System.out.println(Content);
  }
 }
}

This way we can read and write text file In java for your selenium webdriver test case development.

18 comments:

  1. //I need to change PATH like:
    String TestFile = "C:/temp.txt";
    //because I had an error msg with:
    String TestFile = "D:\\temp.txt";

    ReplyDelete
  2. or use double backslash like:
    String TestFile = "C:\\temp\\temp.txt";

    ReplyDelete
  3. Really i feel happy bec of u now i know read and write into file from java... thanks a lot u r so genius....

    ReplyDelete
  4. Great Help! Thanks a lot! God Bless!

    ReplyDelete
  5. Great Help! Many Thanks! God Bless!

    ReplyDelete
  6. Hi Aravind,
    Great work! Thanks.
    One question: do we need to close BR at the last line after println? I was trying to close and getting error. Could you comment?
    Subrata Paul

    ReplyDelete
    Replies
    1. I used BR.close() after while loops finishes. Its working fine. Try that

      Delete
    2. Hey,
      I used BR.close() after while loop finishes. Its working fine. Try that

      Delete
  7. Thanks Aravind, That's a good information. I am trying to append some string value to an existing file. But it erases the existing content and writes the sting as new content. Please help me with the solution. I have used as below.

    FileWriter flw = new FileWriter(lclFile);
    BufferedWriter bfw = new BufferedWriter(flw);
    bfw.append(Content);
    bfw.newLine();
    bfw.close();

    ReplyDelete
  8. @Sowmya.You need to give the string inside the append mehtod not the content.

    ReplyDelete
  9. I did not understand here that how is the testing part shown? Or what testing has been done here?

    ReplyDelete
  10. After G C Reddy & Guru99 .... one of the Best Site for every learners. It is superb. The way he teaches & make us understand is amazing. I am getting the confidence in selenium.

    Thank you Man for Such a wonderful contribution & support.

    ReplyDelete
  11. How to pass username and password from text file to login screen?

    ReplyDelete
  12. Hi
    I want to copy the URL from the chrome and need to save the text file in notepad. Any solution for this?

    ReplyDelete