14 – Java IO Part-2

Selenium Class 14 – Java IO Part-2

Java File handling continuation:

4) Read a Text File

//Open the Specified file in “Read” Mode
FileReader xyz = new FileReader (“C:\Users\admin\Desktop\selenium.txt”);

//Read Data from the opened file
BufferedReader br = new BufferedReader(xyz);
String line;

while ((line =br.readLine()) != null){
System.out.println(line);
}
br.close();
xyz.close();

5) Write Data to a Text file

//Open the Specified file in “Write” Mode
FileWriter file = new FileWriter(“C:\Users\admin\Desktop\selenium.txt”);

//Write data to the opened file
BufferedWriter bw = new BufferedWriter(file);

String data = “Welcome to Selenium World”;
bw.write(data);
bw.close();
file.close();

6) Read a Text File and write to another file

String line;

FileReader file1 = new FileReader (“C:\Users\admin\Desktop\selenium.txt”);
FileWriter file2 = new FileWriter (“C:\Users\admin\Desktop\abc.txt”);

BufferedReader br = new BufferedReader (file1);
BufferedWriter bw = new BufferedWriter (file2);

while ((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
}
bw.close();

br.close();
file2.close();
file1.close();

Assignments:

1) How to conduct Exponentiation Operation?

2) How to handle Date type Data?

3) How to conduct Division Operation?

5) How to find biggest number, and check if one or more numbers are equal?

6) Read a Text File except blank space

7) Create a New File and Write some data

8) Open a Text File and Append some data

9) What is the purpose of Ioexception?

10) What is the purpose of FileNotFoundException?

11) Read a range of data (ex: 4th to 7th lines out of 10 lines) from a text file and write to another file?