0% found this document useful (0 votes)
4 views

JAVA IO

Java I/O (Input and Output) utilizes streams for efficient data processing, with the java.io package providing necessary classes. Key classes include OutputStream and InputStream for writing and reading data, respectively, with FileInputStream and FileOutputStream specifically for file handling. Additional classes like ByteArrayOutputStream and SequenceInputStream allow for advanced data management across multiple streams and files.

Uploaded by

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

JAVA IO

Java I/O (Input and Output) utilizes streams for efficient data processing, with the java.io package providing necessary classes. Key classes include OutputStream and InputStream for writing and reading data, respectively, with FileInputStream and FileOutputStream specifically for file handling. Additional classes like ByteArrayOutputStream and SequenceInputStream allow for advanced data management across multiple streams and files.

Uploaded by

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

Java I/O

• Java I/O (Input and Output) is used to process the


input and produce the output based on the input.
• Java uses the concept of stream to make I/O
operation fast. The java.io package contains all the
classes required for input and output operations.
Stream
• A stream is a sequence of data. In Java a
stream is composed of bytes. It's called a
stream because it's like a stream of water that
continues to flow.
In java, 3 streams are created for us automatically.
All these streams are attached with console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Code to print output and error message to the
console.

System.out.println("simple message");
System.err.println("error message");
OutputStream
Java application uses an output stream to write
data to a destination, it may be a file,an
array,peripheral device or socket.
InputStream
Java application uses an input stream to read
data from a source, it may be a file,an
array,peripheral device or socket.
Working of Java OutputStream and InputStream
OutputStream class
• OutputStream class is an abstract class. It is the super
class of all classes representing an output stream of
bytes. An output stream accepts output bytes and
sends them to some sink.
InputStream class
• InputStream class is an abstract class. It is the
super class of all classes representing an input
stream of bytes.
Commonly used methods of InputStream class
FileInputStream and FileOutputStream (File Handling)

In Java, FileInputStream and FileOutputStream


classes are used to read and write data in file. In
another words, they are used for file handling in
java.
Java FileOutputStream class
Java FileOutputStream is an output stream for
writing data to a file.
If you have to write primitive values then use
FileOutputStream. Instead, for character-oriented
data, prefer FileWriter.But you can write byte-
oriented as well as character-oriented data.
import java.io.*;
class Test{
public static void main(String args[]){
try{
FileOutputstream fout=new FileOutputStream("abc.txt");
String s="Sachin Tendulkar is my favourite player";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e){system.out.println(e);}
}
}
Output:success...
Java FileInputStream class
Java FileInputStream class obtains input bytes from a file.It is used for
reading streams of raw bytes such as image data. For reading streams of
characters, consider using FileReader.It should be used to read byte-
oriented data for example to read image, audio, video etc.
import java.io.*;
class SimpleRead{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
} }
Output:Sachin is my favourite player.
Reading the data of current java file and writing it into
another file
import java.io.*;
class C{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("C.java");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1){
fout.write((byte)i);
}
fin.close();
}
}
We can read the data of any file using the FileInputStream class
whether it is java file, image file, video file etc
Java ByteArrayOutputStream class
• Java ByteArrayOutputStream class is used to
write data into multiple files. In this stream,
the data is written into a byte array that can
be written to multiple stream.
• The ByteArrayOutputStream holds a copy of
data and forwards it to multiple streams.
• The buffer of ByteArrayOutputStream
automatically grows according to data.
ByteArrayOutputStream class to write data into 2 files

import java.io.*;
class S{
public static void main(String args[])throws Exception{
FileOutputStream fout1=new FileOutputStream("f1.txt");
FileOutputStream fout2=new FileOutputStream("f2.txt");
ByteArrayOutputStream bout=new ByteArrayOutputStream();
bout.write(139);
bout.writeTo(fout1);
bout.writeTo(fout2);
bout.flush();
bout.close();//has no effect
System.out.println("success...");
}
}
Java SequenceInputStream class
Java SequenceInputStream class is used to read
data from multiple streams. It reads data of
streams one by one.
Constructor
SequenceInputStream(InputStream s1,
InputStream s2)
creates a new input stream by reading the data
of two input stream in order, first s1 and then
s2.
Example of SequenceInputStream class
import java.io.*;
class Simple{
public static void main(String args[])throws Exception{
FileInputStream fin1=new FileInputStream("f1.txt");
FileInputStream fin2=new FileInputStream("f2.txt");
SequenceInputStream sis=new SequenceInputStream(fin1,fin2);
int i;
while((i=sis.read())!=-1){
System.out.println((char)i);
}
sis.close();
fin1.close();
fin2.close();
}
}

You might also like