How to read and write a data to text files?
1) Reader, InputStreamReader, FileReader and BufferedReader
Reader is the abstract class for reading character streams.
read(): reads a single character.
read(char[]): reads an array of characters.
skip(long): skips some characters.
close(): closes the stream.
2) Writer, OutputStreamWriter, FileWriter and BufferedWriter
Examples:
Reading from Text File Example :
import java.io.FileReader;
import java.io.IOException;
public class TextFileReadingExample1 {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("MyFile.txt");
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextFileReadingExample3 {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writing to Text File Example :
import java.io.FileWriter;
import java.io.IOException;
public class TextFileWritingExample1 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello World");
writer.write("\r\n"); // write new line
writer.write("Good Bye!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class TextFileWritingExample2 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("MyFile.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write("Hello World");
bufferedWriter.newLine();
bufferedWriter.write("See You Again!");
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this Topic, we will see how to read from and write to text files using classes available in the java.io package. First, let’s look at the different classes that are capable of reading and writing character streams.
Reader is the abstract class for reading character streams.
It implements the following fundamental methods:
read(): reads a single character.
read(char[]): reads an array of characters.
skip(long): skips some characters.
close(): closes the stream.
InputStreamReader is a bridge from byte streams to character streams. It converts bytes into characters using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an InputStreamReader.
FileReader is a convenient class for reading text files using the default character encoding of the operating system.
BufferedReader reads text from a character stream with efficiency (characters are buffered to avoid frequently reading from the underlying stream) and provides a convenient method for reading a line of text readLine().
2) Writer, OutputStreamWriter, FileWriter and BufferedWriter
Writer is the abstract class for writing character streams.
It implements the following fundamental methods:
write(int): writes a single character.
write(char[]): writes an array of characters.
write(String): writes a string.
close(): closes the stream.
OutputStreamWriter is a bridge from byte streams to character streams. Characters are encoded into bytes using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an OutputStreamWriter.
FileWriter is a convenient class for writing text files using the default character encoding of the operating system.
BufferedWriter writes text to a character stream with efficiency (characters, arrays and strings are buffered to avoid frequently writing to the underlying stream) and provides a convenient method for writing a line separator: newLine().
Examples:
Reading from Text File Example :
1) The following small program reads every single character from the file MyFile.txt and prints all the characters to the output console:
import java.io.FileReader;
import java.io.IOException;
public class TextFileReadingExample1 {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("MyFile.txt");
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2) Following example uses a BufferedReader to read a text file line by line (this is the most efficient and preferred way.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TextFileReadingExample3 {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writing to Text File Example :
1) In the following example, a FileWriter is used to write two words “Hello World” and “Good Bye!” to a file named MyFile.txt.
import java.io.FileWriter;
import java.io.IOException;
public class TextFileWritingExample1 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello World");
writer.write("\r\n"); // write new line
writer.write("Good Bye!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Note that, a writer uses default character encoding of the operating system by default. It also creates a new file if not exits, or overwrites the existing one. If you want to append text to an existing file, pass a boolean flag of true to constructor of the writer class:
2) Following example uses a BufferedWriter to write in a text file (this is the most efficient and preferred way.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class TextFileWritingExample2 {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("MyFile.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write("Hello World");
bufferedWriter.newLine();
bufferedWriter.write("See You Again!");
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This way we can read and write text file in java for your selenium webdriver test case development.
This concludes the Java tutorial and we will move ahead to selenium webdriver tutorials from next chapter.
No comments:
Post a Comment