Java Programming Notes 2nd Dce
Java Programming Notes 2nd Dce
By Shankhadeep Paral
InputStreamReader Class
// Creates an InputStream
FileInputStream file = new FileInputStream(String path);
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
Here, we have created an InputStreamReader named input along with the FileInputStream
named file.
• Here, the data in the file are stored using some default character encoding.
• However, we can specify the type of character encoding (UTF8 or UTF16) in the file as well.
read() Method
read() - reads a single character from the reader
read(char[] array) - reads the characters from the
reader and stores in the specified array
read(char[] array, int start, int length) - reads the
number of characters equal to length from the reader
and stores in the specified array starting from the start
close() Method
To close the input stream reader, we can use the
close() method. Once the close() method is called,
we cannot use the reader to read the data.
getEncoding()
The getEncoding() method can be used to get the
type of encoding that is used to store data in the
input stream.
• ready() checks if the stream is ready to be read
• mark() mark the position in stream up to which
data has been read
• reset() returns the control to the point in the
stream where the mark was set
import java.io.InputStreamReader;
import java.io.FileInputStream;
public class TestInputStreamReader {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("D:\\ReaderInput.txt");
// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);
catch(Exception e) {
e.getStackTrace();
}
}
}
• In the above example, we have created an input
stream reader using the file input stream. The
input stream reader is linked with the file
input.txt.
FileInputStream file = new FileInputStream("ReaderInput.txt");
InputStreamReader input = new InputStreamReader(file);
• To read characters from the file, we have used
the read() method.
getEncoding() Method
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;
public class TestGetEncoding {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("D:\\ReaderInput.txt");
catch(Exception e) {
e.getStackTrace();
}
}
}
• Here, we have created 2 input stream reader
named input1 and input2.
• input1 does not specify the character encoding.
Hence the getEncoding() method returns the
canonical name of the default character
encoding.
• input2 specifies the character encoding, UTF8.
Hence the getEncoding() method returns the
specified character encoding.
• We have used the Charset.forName() method to
specify the type of character encoding.
OutputStreamWriter Class
// Creates an OutputStreamWriter
OutputStreamWriter output = new OutputStreamWriter(file);
• Here, we have created an OutputStreamWriter named output along with the FileOutputStream
named file.
• Here, we are using the default character encoding to write characters to the output stream.
• However, we can specify the type of character encoding (UTF8 or UTF16) to be used to write data.
try {
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("D:\\output.txt");
// Creates an OutputStreamWriter
OutputStreamWriter output = new OutputStreamWriter(file);
catch (Exception e) {
e.getStackTrace();
}
}
}
• In the above example, we have created an output
stream reader using the file output stream. The
output stream reader is linked with the output.txt
file.
FileOutputStream file = new FileOutputStream("output.txt");
OutputStreamWriter output = new OutputStreamWriter(file);
• To write data to the file, we have used the write()
method.
• output does not specify the character encoding.
Hence the getEncoding() method returns the default
character encoding.
FileReader Class
try {
// Creates a reader using the FileReader
FileReader input = new FileReader("D:\\test.txt");
// Reads characters
input.read(array);
System.out.println("Data in the file: ");
System.out.println(array);
catch(Exception e) {
e.getStackTrace();
}
}
}
• Here, we have created a file reader named input.
The file reader is linked with the file test.txt.
try {
// Creates a FileWriter
FileWriter output = new FileWriter("D:\\output_1.txt");
catch (Exception e) {
e.getStackTrace();
}
}
}
• In the above example, we have created a file
writer named output. The output reader is linked
with the output.txt file.
// Creates a BufferedReader
BufferedReader buffer = new BufferedReader(file);
• Here, we have created a BufferedReader named buffer with the FileReader named file.
• Here, the internal buffer of the BufferedReader has the default size of 8192 characters.
However, we can specify the size of the internal buffer as well.
• read() Method
• read() - reads a single character from the internal
buffer of the reader
• read(char[] array) - reads the characters from the
reader and stores in the specified array
• read(char[] array, int start, int length) - reads the
number of characters equal to length from the
reader and stores in the specified array starting
from the position start
• skip() : To discard and skip the specified number of
characters, we can use the skip() method.
• close() : To close the buffered reader, we can use
the close() method. Once the close() method is
called, we cannot use the reader to read the data.
• ready() checks if the file reader is ready to be read
• mark() mark the position in reader up to which
data has been read
• reset() returns the control to the point in the
reader where the mark was set
• In the above example, we have created a buffered
reader named input. The buffered reader is linked
with the input.txt file.
// Creates a FileWriter
FileWriter file = new FileWriter(String name);
// Creates a BufferedWriter
BufferedWriter buffer = new BufferedWriter(file);
• Here, we have created a BufferedWriter named buffer with the FileWriter named file.
• Here, the internal buffer of the BufferedWriter has the default size of 8192 characters. However,
we can specify the size of the internal buffer as well.
• write() Method
• write() - writes a single character to the internal
buffer of the writer
• write(char[] array) - writes the characters from
the specified array to the writer
• write(String data) - writes the specified string to
the writer
• flush() :To clear the internal buffer, we can use the
flush() method. This method forces the writer to
write all data present in the buffer to the
destination file.
• close() :To close the buffered writer, we can use
the close() method. Once the close() method is
called, we cannot use the writer to write the data.
• newLine() inserts a new line to the writer
• append() inserts the specified character to the
current writer
import java.io.FileWriter;
import java.io.BufferedWriter;
public class TestBufferedWriter {
public static void main(String args[]) {
try {
// Creates a FileWriter
FileWriter file = new FileWriter("D:\\OutputBufferWriter.txt");
// Creates a BufferedWriter
BufferedWriter output = new BufferedWriter(file);
catch (Exception e) {
e.getStackTrace();
}
}
}
• In the above example, we have created a buffered writer
named output along with FileWriter. The buffered writer
is linked with the output.txt file.
// Creates a StringReader
StringReader input = new StringReader(String data);
• Here, we have created a StringReader that reads
characters from the specified string named data.