Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

I/O Streams:-

An I/O Stream represents an input source or an output destination. A stream can represent many
different kinds of sources and destinations, including disk files, devices, other programs, and
memory arrays. Streams support many different kinds of data, including simple bytes, primitive
data types, localized characters, and objects. Some streams simply pass on data; others manipulate
and transform the data in useful ways.
A stream is a sequence of data. A program uses an input stream to read data from a source, one item at
a time:

A program uses an output stream to write data to a destination, one item at time:
Streams:-
 Byte Streams
 Character Streams
Byte streams are used to perform input and output of 8-bit bytes. All byte stream classes are descended from
InputStream and OutputStream. There are many byte stream classes but we’ll focus on FileInputStream and
FileOutputStream.
public class ByteStreamReadAndWrite {
public static void main(String[] args) throws IOException {

try (FileInputStream in = new FileInputStream("D:/f1.txt");


FileOutputStream out = new FileOutputStream("D:/f2.txt")) {
int c;
while ((c = in.read()) != -1) {
out.write(c);}}}}
Note:- In the Byte Streams the int variable holds a byte value in its last 8 bits.
The class ByteStreamReadAndWrite actually represents a kind of low-level I/O that you should avoid.
Since f1.txt contains character data, the best approach is to use character streams.
Character Streams
The Java platform stores character values using Unicode conventions.
Byte streams can read or write the files containing ASCII characters that range from 0 to 255.
That is, byte streams can copy the files containing English letters only but not of other languages.
If the file contains other than English characters, as Java supports Unicode characters, byte
streams fail to do the job. To overcome this, in JDK 1.1 version, designers introduced character
streams. Character streams operate on Unicode characters.

Using Character Streams


All character stream classes are descended from Reader and Writer. As with byte streams, there
are character stream classes that specialize in file I/O: FileReader and FileWriter. The
CopyCharacters.
public class ByteStreamLimitaion{
static void writeOutput(String str) {
try {
FileOutputStream fos = new FileOutputStream("D:/f1.txt");
//fos is not able to write string it is only used to write byte or primitive data type.
Writer out = new OutputStreamWriter(fos, "UTF8");
//UTF8--> Universal Character Set Transformation Format—8-bit
out.write(str);out.close();} catch (IOException e) {e.printStackTrace();}}
static String readInput() {
StringBuffer buffer = new StringBuffer();try {FileInputStream fis = new
FileInputStream("D:/f1.txt");int ch;while ((ch =fis.read()) > -1) {buffer.append((char)
ch);}fis.close();
return buffer.toString();} catch (IOException e) {e.printStackTrace();return null;}}
public static void main(String[] args) {String jaString = new
String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");writeOutput(jaString);String inputString =
readInput();String displayString = jaString + " " + inputString;System.out.println(displayString);}}
In the above code we lose the data because to read data we are using byte stream class that
support only ASCII characters that range from 0 to 255 but data is in other language that support
Unicode Character. So to support the UCS use CharacterStream.Ex:-
public class CharacterStraemSupportUCS{
static void writeOutput(String str) {
try {FileOutputStream fos = new FileOutputStream("D:/f1.txt");
Writer out = new OutputStreamWriter(fos, "UTF8");out.write(str);out.close();
} catch (IOException e) {e.printStackTrace();}}
static String readInput() {
StringBuffer buffer = new StringBuffer();try {FileInputStream fis = new
FileInputStream("D:/f1.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);int ch;while ((ch = in.read()) > -1) {buffer.append((char)
ch);}in.close();return buffer.toString();} catch (IOException e) {e.printStackTrace();return
null;}}public static void main(String[] args) {
String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");
writeOutput(jaString);
String inputString = readInput();
String displayString = jaString + " " + inputString;
System.out.println(displayString);}}
Note:- In the Character Stream the int variable holds a byte value in its last 32 bits. The character
stream uses the byte stream to perform the physical I/O, while the character stream handles
translation between characters and bytes. FileReader, for example, uses FileInputStream, while
FileWriter uses FileOutputStream.
Buffered Stream:-
Reading data from disk byte-by-byte is very inefficient. One way to speed it up is to use a buffer: instead
of reading one byte at a time, you read a few thousand bytes at once, and put them in a buffer, in
memory. Then you can look at the bytes in the buffer one by one.
Reading Characters
read() method is used with BufferedReader object to read characters. As this function returns integer
type value has we need to use typecasting to convert it into.
int read() throws IOException

Reading Strings:-
To read string we have to use readLine() function with BufferedReader class's object.
String readLine() throws IOException

public class BufferedReaderDemo{


public static void main(String[] args) throws IOException {

try (BufferedReader in = new BufferedReader(new FileReader("D:/f1.txt"));


BufferedWriter out = new BufferedWriter(new FileWriter("D:/f2.txt"))) {
String readData=in.readLine();
out.write(readData);}}}
I/O from the Command Line
A program is often run from the command line and interacts with the user in the command line
environment. The Java platform supports this kind of interaction in two ways: through the Standard
Streams and through the Console.
Standard Streams
Standard Streams are a feature of many operating systems. By default, they read input from the
keyboard and write output to the display. They also support I/O on files
The Java platform supports three Standard Streams: Standard Input, accessed through System.in;
Standard Output, accessed through System.out; and Standard Error, accessed through System.err.
These objects are defined automatically and do not need to be opened. Standard Output and
Standard Error are both for output; having error output separately allows the user to divert regular
output to a file and still be able to read error messages.
System.out and System.err are defined as PrintStream objects.
Thanks…

You might also like