We have already dealt with Exceptions in Selenium now we will focus on how to open/write/read to a text file. Further we would utilize the learning in our real life example of reading all the links on page and write it into a text file using Selenium.
Don’t forgot to like our FB page to learn more.
![]() |
Learn working with Files |
Important Tips for Reading a File:-
1.While working with files, we need to handle the Checked Exception in our code, otherwise our code will not compile.
2.FileReader class makes it possible to read the contents of a file as a stream of characters, thus we are using in our code to read characters from a text file.
FileReader fr = new FileReader(filePath)
3.BufferedReader class provides buffering to our FileReader. Rather than reading one character at a time from the file, we read a larger block at a time using BufferedReader.
FileReader fr = new FileReader(filePath)
BufferedReader txtReader = new BufferedReader(fr)
We can specify the buffer size in the above case we have used the default size(8192 chars).
Incase we want sized buffer our code will look like this:-
Int bufferSize = 1000
BufferedReader txtReader = new BufferedReader(fr, bufferSize)
4.ReadLine, method of BufferedReader Class, which reads a line of text. A line is considered to be terminated by any one of a line feed (‘n’), a carriage return (‘r’), or a carriage return followed immediately by a linefeed.
Code:-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class WorkingwithFiles {
public static void main(String[] args) {
//FilePath
String sFilePath = "D:\Automation\Selenium\TestFile.txt";
//Creating FileReader object
FileReader fr = null;
//Creating BufferedReader object
BufferedReader txtReader = null;
//Handling Exception using Try-Catch
try {
String sCurrentLine;
fr = new FileReader(sFilePath);
txtReader = new BufferedReader(fr);
//Reading file until file is null
while ((sCurrentLine = txtReader.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (txtReader != null)txtReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Important Tips for Writing into a File:-
1.FileWriter class makes it possible to write a file as a stream of characters, same like FileReader above.
FileWriter fw = new FileWriter(filepath)
2.Incase we want to control whether to append or overwrite into text file
Boolean append = True
FileWriter fw = new FileWriter(filepath, append)
By Default it is “False”, thus it will overwrite the existing data.
3.BufferedWriter class provides buffering to our Write. Rather than write one character at a time to the file, we write a larger block at a time. This is typically much faster, especially for disk access and larger data amounts
FileWriter fw = new FileWriter(path)
BufferedWriter bw = new BufferedWriter (fw)
We can control the size of bufferedwriter like we did in bufferedreader class
4.We used File objects to obtain information about a file or directory.File object is used to obtain information about a particular file or directory and is not used to read or write data, like length of a file
Code:-
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WritingInFiles {
public static void main(String[] args) {
//Data to write
String sContent = "Lets try to write newline into File";
//FilePath
String sFilePath = "D:\Automation\Selenium\TestFile.txt";
try {
//Creating File object
File file = new File(sFilePath);
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//Creating FileWriter object
//using file object we got the filePath
FileWriter fw = new FileWriter(file.getAbsoluteFile());
//Creating BufferedWriter object
BufferedWriter bw = new BufferedWriter(fw);
//Writing content into file
bw.write(sContent);
//Adding new line
bw.newLine();
bw.close();
System.out.println("Data is Successfully written");
} catch (IOException e) {
e.printStackTrace();
}
}
}