Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Java Uni5

The document discusses different Java classes used for reading and writing character streams like Reader, InputStreamReader, FileReader, BufferedReader, Writer, OutputStreamWriter, FileWriter and BufferedWriter. It explains their relationships and how to use them along with character encoding and reading/writing files as examples.

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Uni5

The document discusses different Java classes used for reading and writing character streams like Reader, InputStreamReader, FileReader, BufferedReader, Writer, OutputStreamWriter, FileWriter and BufferedWriter. It explains their relationships and how to use them along with character encoding and reading/writing files as examples.

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Reader, InputStreamReader, FileReader and


BufferedReader
Readeris 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().
The following diagram show relationship of these reader classes in the java.io package:

2. Writer, OutputStreamWriter, FileWriter and BufferedWriter

writeris 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().

3. Character Encoding and Charset


When constructing a reader or writer object, the default character encoding of the operating system
is used (e.g. Cp1252 on Windows):
1 FileReader reader = new FileReader("MyFile.txt");
2 FileWriter writer = new FileWriter("YourFile.txt");
So if we want to use a specific charset, use
an InputStreamReader or OutputStreamWriter instead. For example:
1 InputStreamReader reader = new InputStreamReader(
2 new FileInputStream("MyFile.txt"), "UTF-16");

That creates a new reader with the Unicode character encoding UTF-16.
And the following statement constructs a writer with the UTF-8 encoding:
1 OutputStreamWriter writer = new OutputStreamWriter(
2 new FileOutputStream("YourFile.txt"), "UTF-8");

In case we want to use a BufferedReader, just wrap the InputStreamReader inside, for example:
1 InputStreamReader reader = new InputStreamReader(
2 new FileInputStream("MyFile.txt"), "UTF-16");
3
4 BufferedReader bufReader = new BufferedReader(reader);

And for a BufferedWriter example:


1 OutputStreamWriter writer = new OutputStreamWriter(
2 new FileOutputStream("YourFile.txt"), "UTF-8");
3
4 BufferedWriter bufWriter = new BufferedWriter(writer);

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;

/**
* This program demonstrates how to read characters from a text file.
*/
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();
}
}
}

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;

/**
* This program demonstrates how to write characters to a text file.
*
*/
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();
}

You might also like