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.
super bro.....
ReplyDelete//I need to change PATH like:
ReplyDeleteString TestFile = "C:/temp.txt";
//because I had an error msg with:
String TestFile = "D:\\temp.txt";
or use double backslash like:
ReplyDeleteString TestFile = "C:\\temp\\temp.txt";
Really i feel happy bec of u now i know read and write into file from java... thanks a lot u r so genius....
ReplyDeleteGreat Help! Thanks a lot! God Bless!
ReplyDeleteGreat Help! Many Thanks! God Bless!
ReplyDeleteHi Aravind,
ReplyDeleteGreat 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
I used BR.close() after while loops finishes. Its working fine. Try that
DeleteHey,
DeleteI used BR.close() after while loop finishes. Its working fine. Try that
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.
ReplyDeleteFileWriter flw = new FileWriter(lclFile);
BufferedWriter bfw = new BufferedWriter(flw);
bfw.append(Content);
bfw.newLine();
bfw.close();
very useful
ReplyDelete@Sowmya.You need to give the string inside the append mehtod not the content.
ReplyDeleteI did not understand here that how is the testing part shown? Or what testing has been done here?
ReplyDeleteAfter 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.
ReplyDeleteThank you Man for Such a wonderful contribution & support.
How to pass username and password from text file to login screen?
ReplyDeleteHi
ReplyDeleteI want to copy the URL from the chrome and need to save the text file in notepad. Any solution for this?
really helpful code
ReplyDeleteHelpful post
ReplyDelete