0% found this document useful (0 votes)
48 views7 pages

Java - Streams, Files and I/O

1) Streams in Java can be used for input and output of data and are divided into byte streams and character streams. Byte streams handle bytes of data while character streams handle Unicode characters. 2) Common stream classes for files include FileInputStream, FileOutputStream, FileReader, and FileWriter. FileInputStream and FileOutputStream handle bytes while FileReader and FileWriter handle characters. 3) Java also supports standard input, output, and error streams accessed through System.in, System.out, and System.err respectively.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Java - Streams, Files and I/O

1) Streams in Java can be used for input and output of data and are divided into byte streams and character streams. Byte streams handle bytes of data while character streams handle Unicode characters. 2) Common stream classes for files include FileInputStream, FileOutputStream, FileReader, and FileWriter. FileInputStream and FileOutputStream handle bytes while FileReader and FileWriter handle characters. 3) Java also supports standard input, output, and error streams accessed through System.in, System.out, and System.err respectively.

Uploaded by

Praneeth Mutnuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java - Streams, Files and I/O

A stream can be defined as a sequence of data. The Input Stream is used to read
data from a source and the Output Stream is used for writing data to a destination.
Java provides strong but flexible support for I/O related to Files and networks but
this tutorial covers very basic functionality related to streams and I/O. We would
see most commonly used example one by one:
Byte Streams:
Java byte streams are used to perform input and output of 8-bit bytes. Though there
are many classes related to byte streams but the most frequently used classes are
,FileInputStream and FileOutputStream. Following is an example which makes use
of these two classes to copy an input file into an output file:

import java.io.*;
publicclassCopyFile{
publicstaticvoid main(Stringargs[])throwsIOException
{
FileInputStreamin=null;
FileOutputStreamout=null;
try{
in=newFileInputStream("input.txt");
out=newFileOutputStream("output.txt");
int c;
while((c =in.read())!=-1){
out.write(c);
}
}finally{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
}
}
Character Streams:
Java Byte streams are used to perform input and output of 8-bit bytes, where as
Java Character streams are used to perform input and output for 16-bit unicode.
Though there are many classes related to character streams but the most frequently
used classes are ,File Reader and File Writer.. Though internally File Reader uses
FileInputStream and File Writer uses FileOutputStream but here major difference
is that File Reader reads two bytes at a time and File Writer writes two bytes at a
time.
We can re-write above example which makes use of these two classes to copy an
input file (having unicode characters) into an output file:
import java.io.*;

public class CopyFile{


publicstaticvoid main(String args[])throwsIOException
{
FileReader in=null;
FileWriter out=null;

try{
in=newFileReader("input.txt");
out=newFileWriter("output.txt");

int c;
while((c =in.read())!=-1){
out.write(c);
}
}finally{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
}
}
Standard Streams:
All the programming languages provide support for standard I/O where user's
program can take input from a keyboard and then produce output on the computer
screen. If you are aware if C or C++ programming languages, then you must be
aware of three standard devices STDIN, STDOUT and STDERR. Similar way Java
provides following three standard streams
 Standard Input: This is used to feed the data to user's program and usually
a keyboard is used as standard input stream and represented as System.in.
 Standard Output: This is used to output the data produced by the user's
program and usually a computer screen is used to standard output stream and
represented as System.out.
 Standard Error: This is used to output the error data produced by the user's
program and usually a computer screen is used to standard error stream and
represented as System.err.
Following is a simple program which creates InputStreamReader to read standard
input stream until the user types a "q":
import java.io.*;

publicclassReadConsole{
publicstaticvoid main(Stringargs[])throwsIOException
{
InputStreamReadercin=null;

try{
cin=newInputStreamReader(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
do{
c =(char)cin.read();
System.out.print(c);
}while(c !='q');
}finally{
if(cin!=null){
cin.close();
}
}
}
}
Reading and Writing Files:
A stream can be defined as a sequence of data. The Input Stream is used to read
data from a source and the Output Stream is used for writing data to a destination.
Here is a hierarchy of classes to deal with Input and Output streams.
FileInputStream:
This stream is used for reading data from the files. Objects can be created using the
keyword new and there are several types of constructors available.

Following constructor takes a file name as a string to create an input stream object
to read the file.:

InputStream f =newFileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the
file. First we create a file object using File() method as follows:
File f =newFile("C:/java/hello");
InputStream f =newFileInputStream(f);

FileOutputStream:
FileOutputStream is used to create a file and write data into it. The stream would
create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object
to write the file:
OutputStream f =newFileOutputStream("C:/java/hello")
Following constructor takes a file object to create an output stream object to write
the file. First, we create a file object using File() method as follows:
File f =newFile("C:/java/hello");
OutputStream f =newFileOutputStream(f);
Following is the example to demonstrate InputStream and OutputStream:
import java.io.*;

public class fileStreamTest{

public static void main(String args[]){

try{
bytebWrite [] = {11,21,3,40,5};
OutputStreamos = new FileOutputStream("test.txt");
for(int x=0; x <bWrite.length ; x++){
os.write(bWrite[x] ); // writes the bytes
}
os.close();

InputStream is = new FileInputStream("test.txt");


int size = is.available();

for(inti=0; i< size; i++){


System.out.print((char)is.read() + " ");
}
is.close();
}catch(IOException e){
System.out.print("Exception");
}
}
}

There are other important input streams available, for more detail you can refer to
the following links:
 ByteArrayInputStream
 DataInputStream
The ByteArrayOutputStream class stream creates a buffer in memory and all the
data sent to the stream is stored in the buffer. There are following forms of
constructors to create ByteArrayOutputStream objects
Following constructor creates a buffer of 32 byte:
OutputStreambOut = new ByteArrayOutputStream()
Following constructor creates a buffer of size int a:
OutputStreambOut = new ByteArrayOutputStream(int a);
Following is the example to demonstrate ByteArrayOutputStream and
ByteArrayOutputStream

import java.io.*;
public class ByteStreamTest {
public static void main(String args[])throws IOException {
ByteArrayOutputStreambOutput = new ByteArrayOutputStream(12);
while(bOutput.size()!= 10 ) {
// Gets the inputs from the user
bOutput.write(System.in.read());
}
byte b [] = bOutput.toByteArray();
System.out.println("Print the content");
for(int x= 0 ; x <b.length; x++) {
//printing the characters
System.out.print((char)b[x] + " ");
}
System.out.println(" ");
int c;
ByteArrayOutputStreambInput = new ByteArrayOutputStream(b);
System.out.println("Converting characters to Upper case " );
for(int y = 0 ; y < 1; y++ ) {
while(( c= bInput.read())!= -1) {
System.out.println(Character.toUpperCase((char)c));
}
bInput.reset();
}
}
}

The DataOutputStream stream let you write the primitives to an output source.
Following is the constructor to create a DataOutputStream.
DataOutputStream out = DataOutputStream(OutputStream out);
Following is the example to demonstrate DataInputStream and DataInputStream.

import java.io.*;
public class Test{
public static void main(String args[])throws IOException{
DataInputStream d = new DataInputStream(new FileInputStream("test.txt"));
DataOutputStream out = new DataOutputStream (newFileOutputStream
("test1.txt"));
String count;
while((count = d.readLine()) != null){
String u = count.toUpperCase();
System.out.println(u);
out.writeBytes(u + " ,");
}
d.close();
out.close();
}
}

You might also like