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

Java Programming Notes 2nd Dce

The InputStreamReader and OutputStreamWriter classes allow conversion between byte streams and character streams. InputStreamReader reads bytes and converts them to characters, while OutputStreamWriter writes characters and converts them to bytes. Both extend abstract classes and work with other input/output streams. They provide a bridge between byte and character data. The FileReader class reads character data from files using the Reader methods.

Uploaded by

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

Java Programming Notes 2nd Dce

The InputStreamReader and OutputStreamWriter classes allow conversion between byte streams and character streams. InputStreamReader reads bytes and converts them to characters, while OutputStreamWriter writes characters and converts them to bytes. Both extend abstract classes and work with other input/output streams. They provide a bridge between byte and character data. The FileReader class reads character data from files using the Reader methods.

Uploaded by

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

Java Programming

By Shankhadeep Paral
InputStreamReader Class

 The InputStreamReader class of the java.io


package can be used to convert data in bytes
into data in characters.
 It extends the abstract class Reader.
• The InputStreamReader class works with other input
streams. It is also known as a bridge between byte
streams and character streams. This is because the
InputStreamReader reads bytes from the input stream
as characters.

• For example, some characters required 2 bytes to be


stored in the storage. To read such data we can use
the input stream reader that reads the 2 bytes
together and converts into the corresponding
character.
Create an InputStreamReader
• In order to create an InputStreamReader, we must import the java.io.InputStreamReader
package first. Once we import the package here is how we can create the input stream
reader.

// 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.

// Creates an InputStreamReader specifying the character encoding


• InputStreamReader input = new InputStreamReader(file, Charset cs);
• Here, we have used the Charset class to specify the character encoding in the file.
Methods of InputStreamReader
The InputStreamReader class provides implementations
for different methods present in the Reader class.

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) {

// Creates an array of character


char[] array = new char[100];

try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("D:\\ReaderInput.txt");

// Creates an InputStreamReader
InputStreamReader input = new InputStreamReader(file);

// Reads characters from the file


input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);

// Closes the reader


input.close();
}

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");

// Creates an InputStreamReader with default encoding


InputStreamReader input1 = new InputStreamReader(file);

// Creates an InputStreamReader specifying the encoding


InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));

// Returns the character encoding of the input stream


System.out.println("Character encoding of input1: " + input1.getEncoding());
System.out.println("Character encoding of input2: " + input2.getEncoding());

// Closes the reader


input1.close();
input2.close();
}

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

• The OutputStreamWriter class of the java.io


package can be used to convert data in
character form into data in bytes form.
• It extends the abstract class Writer.
• The OutputStreamWriter class works with other
output streams. It is also known as a bridge
between byte streams and character streams.
This is because the OutputStreamWriter converts
its characters into bytes.
Create an OutputStreamWriter
• In order to create an OutputStreamWriter, we must import the java.io.OutputStreamWriter
package first. Once we import the package here is how we can create the output stream writer.
// Creates an OutputStream
FileOutputStream file = new FileOutputStream(String path);

// 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.

// Creates an OutputStreamWriter specifying the character encoding


OutputStreamWriter output = new OutputStreamWriter(file, Charset cs);
• Here, we have used the Charset class to specify the type of character encoding.
Methods of OutputStreamWriter

• The OutputStreamWriter class provides


implementations for different methods present
in the Writer class.
write() Method
write() - writes a single character to 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
• getEncoding() method can be used to get the
type of encoding that is used to write data to the
output stream.
close() Method
• To close the output stream writer, we can use
the close() method. Once the close() method is
called, we cannot use the writer to write the
data.
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class TestOutputStreamWriter {
public static void main(String args[]) {

String data = "This is an example of OutputStreamWriter.";

try {
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("D:\\output.txt");

// Creates an OutputStreamWriter
OutputStreamWriter output = new OutputStreamWriter(file);

// Writes string to the file


output.write(data);

// Closes the writer


output.close();
}

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

• The FileReader class of the java.io package can


be used to read data (in characters) from files.

• It extends the InputSreamReader class.


Create a FileReader

• In order to create a file reader, we must import


the java.io.FileReader package first. Once we
import the package, here is how we can create
the file reader.
• Using the name of the file
FileReader input = new FileReader(String name);
• Here, we have created a file reader that will be
linked to the file specified by the name.
• Here,the data in the file are stored using some
default character encoding.

• However, since Java 11 we can specify the type


of character encoding (UTF-8 or UTF-16) in the
file as well.
FileReader input = new FileReader(String file, Charset cs);
• Here, we have used the Charset class to specify
the character encoding of the file reader.
Methods of FileReader

The FileReader class provides implementations for


different methods present in the Reader class.
• 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 position start
import java.io.FileReader;
public class TestFileReader {
public static void main(String[] args) {

// Creates an array of character


char[] array = new char[100];

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

// Closes the reader


input.close();
}

catch(Exception e) {
e.getStackTrace();
}
}
}
• Here, we have created a file reader named input.
The file reader is linked with the file test.txt.

FileInputStream input = new FileInputStream(“test.txt");


• To read data from the file, we have used the
read() method.
FileWriter Class

• The FileWriter class of the java.io package can be


used to write data (in characters) to files.

• It extends the OutputStreamWriter class.


Create a FileWriter
• In order to create a file writer, we must import the
Java.io.FileWriter package first. Once we import the
package, here is how we can create the file writer.

• Using the name of the file

FileWriter output = new FileWriter(String name);


• Here, we have created a file writer that will be
linked to the file specified by the name.
Methods of FileWriter

• The FileWriter class provides implementations


for different methods present in the Writer class.
• write() - writes a single character to 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
• getEncoding() method can be used to get the
type of encoding that is used to write data.
• close() Method
• To close the file writer, we can use the close()
method. Once the close() method is called, we
cannot use the writer to write the data.
• flush() forces to write all the data present in
the writer to the corresponding destination
• append() inserts the specified character to the
current writer
import java.io.FileWriter;
public class TestFileWriter {
public static void main(String args[]) {

String data = "This is the data in the output file";

try {
// Creates a FileWriter
FileWriter output = new FileWriter("D:\\output_1.txt");

// Writes the string to the file


output.write(data);

// Closes the writer


output.close();
}

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.

• FileWriter output = new FileWriter("output.txt");


• To write data to the file, we have used the write()
method.
Java BufferedReader

• The BufferedReader class of the java.io package


can be used with other readers to read data (in
characters) more efficiently.

• It extends the abstract class Reader.


Working of BufferedReader

• The BufferedReader maintains an internal buffer


of 8192 characters.
• During the read operation in BufferedReader, a
chunk of characters is read from the disk and
stored in the internal buffer. And from the
internal buffer characters are read individually.

• Hence, the number of communication to the disk


is reduced. This is why reading characters is faster
using BufferedReader.
Create a BufferedReader
• In order to create a BufferedReader, we must import the java.io.BuferedReader
package first. Once we import the package, here is how we can create the reader.
// Creates a FileReader
FileReader file = new FileReader(String 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.

// Creates a BufferdReader with specified size internal buffer


BufferedReader buffer = new BufferedReader(file, int size);
• The buffer will help to read characters from the files more quickly.
Methods of BufferedReader

• 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.

• FileReader file = new FileReader("input.txt");


• BufferedReader input = new BufferedReader(file);
• Here, we have used the read() method to read an
array of characters from the internal buffer of the
buffered reader.
BufferedWriter Class

• The BufferedWriter class of the java.io package


can be used with other writers to write data (in
characters) more efficiently.

• It extends the abstract class Writer.


Working of BufferedWriter
• The BufferedWriter maintains an internal buffer of 8192
characters.

• During the write operation, the characters are written


to the internal buffer instead of the disk. Once the
buffer is filled or the writer is closed, the whole
characters in the buffer are written to the disk.

• Hence, the number of communication to the disk is


reduced. This is why writing characters is faster using
BufferedWriter.
Create a BufferedWriter
• In order to create a BufferedWriter, we must import the java.io.BufferedWriter package first.
Once we import the package here is how we can create the buffered writer.

// 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.

// Creates a BufferedWriter with specified size internal buffer


• BufferedWriter buffer = new BufferedWriter(file, int size);
• The buffer will help to write characters to the files more efficiently.
Methods of BufferedWriter

• 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[]) {

String data = "This is the data in the output file";

try {
// Creates a FileWriter
FileWriter file = new FileWriter("D:\\OutputBufferWriter.txt");

// Creates a BufferedWriter
BufferedWriter output = new BufferedWriter(file);

// Writes the string to the file


output.write(data);

// Closes the writer


output.close();
}

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.

FileWriter file = new FileWriter(" OutputBufferWriter.txt ");


BufferedWriter output = new BufferedWriter(file);
• To write data to the file, we have used the write()
method.

• Here when we run the program, the output.txt file is


filled with the following content.
StringReader

• In order to create a StringReader, we must import


the java.io.StringReader package first. Once we
import the package here is how we can create the
string reader.

// Creates a StringReader
StringReader input = new StringReader(String data);
• Here, we have created a StringReader that reads
characters from the specified string named data.

You might also like