Java - Input-Output
Java - Input-Output
Java I/O :
Java I/O (Input and Output) is used to process the input
and produce the output.
Stream :
A stream is a sequence of data. In Java, a stream is
composed of bytes. It's called a stream because it is like a
stream of water that continues to flow.
Stream :
Let's see the code to print output and an error message to the
console.
System.out.println("simple message");
System.err.println("error message");
OutputStream vs InputStream :
OutputStream :
InputStream :
OutputStream class :
OutputStream class is an abstract class. It is the superclass of
all classes representing an output stream of bytes. An output
stream accepts output bytes and sends them to some sink.
Method Description
1) public void write(int)throws IOException is used to write a byte to the
current output stream.
2) public void write(byte[])throws IOException is used to write an array of byte
to the current output stream.
3) public void flush()throws IOException flushes the current output
stream.
4) public void close()throws IOException is used to close the current
output stream.
Java I/O
OutputStream Hierarchy
Java I/O
InputStream class
InputStream class is an abstract class. It is the superclass of all
classes representing an input stream of bytes.
Method Description
1) public abstract int read()throws reads the next byte of data from
IOException the input stream. It returns -1 at the
end of the file.
2) public int available()throws IOException returns an estimate of the number
of bytes that can be read from the
current input stream.
3) public void close()throws IOException is used to close the current input
stream.
4) public void close()throws IOException is used to close the current output
stream.
Java I/O
InputStream Hierarchy
Java I/O
testout.txt
A
Java I/O
Success...
The content of a text file testout.txt is set with the data Welcome
to javaTpoint.
testout.txt
Welcome to KIIT.
Java I/O
Method Description
int available() It is used to return the estimated number
of bytes that can be read from the input
stream.
Method Description
FileChannel getChannel() It is used to return the unique
FileChannel object associated with the
file input stream.
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
Java I/O
Welcome to KIIT.
Output:
W
Java I/O
Welcome to KIIT.
Java I/O
PrintWriter class :
Java PrintWriter class is the implementation of Writer class. It is
used to print the formatted representation of objects to the
text-output stream.