IO Stream
IO Stream
IO Stream
IO Stream
Java I/O
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.
OutputStream vs InputStream
(Contd…)
OutputStream class
1.import java.io.FileOutputStream;
2.public class FileOutputStreamExample {
3. public static void main(String args[]){
4. try{
5. FileOutputStream fout=new FileOutputStream("D:\\
testout.txt");
6. fout.write(65);
7. fout.close();
8. System.out.println("success...");
9. }catch(Exception e){System.out.println(e);}
10. }
11.}
Output:
success…
Java FileOutputStream example 2: write string
1.import java.io.FileOutputStream;
2.public class FileOutputStreamExample {
3. public static void main(String args[]){
4. try{
5. FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
6. String s="Welcome to Class.";
7. byte b[]=s.getBytes();//converting string into byte array
8. fout.write(b);
9. fout.close();
10. System.out.println("success...");
11. }catch(Exception e){System.out.println(e);}
12. }
13.}
Output:
success…
Java FileInputStream Class
int read() It is used to read the byte of data from the input stream.
It is used to read up to b.length bytes of data from the input
int read(byte[] b)
stream.
int read(byte[] b, int
It is used to read up to len bytes of data from the input stream.
off, int len)
It is used to skip over and discards x bytes of data from the input
long skip(long x)
stream.
FileChannel It is used to return the unique FileChannel object associated with
getChannel() the file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
protected void It is used to ensure that the close method is call when there is no
finalize() more reference to the file input stream.
void close() It is used to closes the stream.
Java FileInputStream example 1: read single character
1.import java.io.FileInputStream;
2.public class DataStreamExample {
3. public static void main(String args[]){
4. try{
5. FileInputStream fin=new FileInputStream("D:\\testout.txt");
6. int i=fin.read();
7. System.out.print((char)i);
8.
9. fin.close();
10. }catch(Exception e){System.out.println(e);}
11. }
12. }
Output:
W
Java FileInputStream example 2: read all
characters
1.import java.io.FileInputStream;
2.public class DataStreamExample {
3. public static void main(String args[]){
4. try{
5. FileInputStream fin=new FileInputStream("D:\\testout.txt");
6. int i=0;
7. while((i=fin.read())!=-1){
8. System.out.print((char)i);
9. }
10. fin.close();
11. }catch(Exception e){System.out.println(e);}
12. }
13. }
Output:
Welcome to Class
Java BufferedOutputStream Class
Method Description
1.import java.io.*;
2.public class BufferedOutputStreamExample{
3.public static void main(String args[])throws Exception{
4. FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
5. BufferedOutputStream bout=new BufferedOutputStream(fout);
6. String s="Welcome to Class.";
7. byte b[]=s.getBytes();
8. bout.write(b);
9. bout.flush();
10. bout.close();
11. fout.close();
12. System.out.println("success");
13.}
14.}
Output:
success…
Java BufferedInputStream Class
Constructor Description
It creates the BufferedInputStream
BufferedInputStream(InputStream
and saves it argument, the input
IS)
stream IS, for later use.
It creates the BufferedInputStream
BufferedInputStream(InputStream with a specified buffer size and
IS, int size) saves it argument, the input stream
IS, for later use.
Java BufferedInputStream Class
(Contd…)
Java BufferedInputStream class methods
Method Description
It returns an estimate number of bytes that can be read from
int available() the input stream without blocking by the next invocation
method for the input stream.
int read() It read the next byte of data from the input stream.
int read(byte[] b, int It read the bytes from the specified byte-input stream into a
off, int ln) specified byte array, starting with the given offset.
It closes the input stream and releases any of the system
void close()
resources associated with the stream.
It repositions the stream at a position the mark method was last
void reset()
called on this input stream.
void mark(int It sees the general contract of the mark method for the input
readlimit) stream.
long skip(long x) It skips over and discards x bytes of data from the input stream.
boolean It tests for the input stream to support the mark and reset
markSupported() methods.
Example of Java
BufferedInputStream
1.import java.io.*;
2.public class BufferedInputStreamExample{
3. public static void main(String args[]){
4. try{
5. FileInputStream fin=new FileInputStream("D:\\testout.txt");
6. BufferedInputStream bin=new BufferedInputStream(fin);
7. int i;
8. while((i=bin.read())!=-1){
9. System.out.print((char)i);
10. }
11. bin.close();
12. fin.close();
13. }catch(Exception e){System.out.println(e);}
14. }
15.}
Output:
Welcome to Class
Java Writer
Constructor
Java Reader is an abstract class for reading character streams. The only
methods that a subclass must implement are read(char[], int, int) and
close(). Most subclasses, however, will override some of the methods to
provide higher efficiency, additional functionality, or both.
Some of the implementation class are BufferedReader, CharArrayReader,
FilterReader, InputStreamReader, PipedReader, StringReader
Fields
Constructor
Method Description
Java FileReader class is used to read data from the file. It returns data in
byte format like FileInputStream class.
It is character-oriented class which is used for file handling in java.
Java FileReader class declaration
1. public class FileReader extends InputStreamReader
Constructors of FileReader class
Constructor Description
thod Description
Output:
Welcome to Class.
Java BufferedWriter Class
Constructor Description
Class methods
Method Description
It is used to add a new line by writing a
void newLine()
line separator.
void write(String s, int off, int len) It is used to write a portion of a string.
1.import java.io.*;
2.public class BufferedWriterExample {
3.public static void main(String[] args) throws Exception {
4. FileWriter writer = new FileWriter("D:\\testout.txt");
5. BufferedWriter buffer = new BufferedWriter(writer);
6. buffer.write("Welcome to Class.");
7. buffer.close();
8. System.out.println("Success");
9. }
10.}
Output: testout.txt:
Success
Welcome to Class.
Java BufferedReader Class