Working with Text Files in Selenium using Java – User friendly Tech help

Today we are going to focus on learning to work with Text Files in Selenium
nWe 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.
n
nDon’t forgot to like our FB page to learn more.
n
n

n

n

n

n

n

n

n

n

Learn working with Files

n

nImportant Tips for Reading a File:-
n
1.While working with files, we need to handle the Checked Exception in our code, otherwise our code will not compile.
n
n2.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.

n

FileReader fr = new FileReader(filePath)

n

n3.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.
n

n

FileReader fr = new FileReader(filePath)
BufferedReader txtReader = new BufferedReader(fr)

n


n

We can specify the buffer size in the above case we have used the default size(8192 chars).
nIncase we want sized buffer our code will look like this:-
n

n

Int bufferSize = 1000
BufferedReader txtReader = new BufferedReader(fr, bufferSize)

n

n4.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.
n
nCode:-
n

n

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(); } } }}

n

Important Tips for Writing into a File:-
n
n1.FileWriter class makes it possible to write a file as a stream of characters, same like FileReader above.
n

n

FileWriter fw = new FileWriter(filepath)

n

2.Incase we want to control whether to append or overwrite into text file

n

Boolean append = True
FileWriter fw = new FileWriter(filepath, append)

n


n

By Default it is “False”, thus it will overwrite the existing data.
n
n3.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
n

n

FileWriter fw = new FileWriter(path)
BufferedWriter bw = new BufferedWriter (fw)

n

nWe can control the size of bufferedwriter like we did in bufferedreader class
n
n4.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
n
nCode:-
n

n

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(); } }}
Was this article helpful?
YesNo

Similar Posts