inputoutput in java
inputoutput in java
Java brings various Streams with its I/O package that helps the user to perform all the input-
output operations. These streams support all the types of objects, data-types, characters, files etc
to fully execute the I/O operations.
Before exploring various input and output streams lets look at 3 standard or default streams that
Java has to provide which are also most common in use:
System.in: This is the standard input stream that is used to read characters from the keyboard or
any other standard input device.
System.out: This is the standard output stream that is used to produce the result of a program on
an output device like the computer screen.
Here is a list of the various print functions that we use to output statements:
print(): This method in Java is used to display a text on the console. This text is passed as the
parameter to this method in the form of String. This method prints the text on the console and the
cursor remains at the end of the text at the console. The next printing takes place from just here.
Syntax:
System.out.print(parameter);
Example:
Import java.io.*;
class Demo_print {
// using print()
// same line
System.out.print("GfG! ");
System.out.print("GfG! ");
System.out.print("GfG! ");
Output:
println(): This method in Java is also used to display a text on the console. It prints the text on
the console and the cursor moves to the start of the next line at the console. The next printing
takes place from the next line.
Syntax:
System.out.println(parameter);
Example:
class Demo_print {
// using println()
// different line
System.out.println("GfG! ");
System.out.println("GfG! ");
System.out.println("GfG! ");
Output:
GfG!
GfG!
GfG!
printf(): This is the easiest of all methods as this is similar to printf in C. Note that
System.out.print() and System.out.println() take a single argument, but printf() may take multiple
arguments. This is used to format the output in Java.
Example:
Class JavaFormatter1 {
intx = 100;
// 2 decimal places
floatn = 5.2f;
n = 2324435.3f;
// width of 20 characters
Output:
System.err: This is the standard error stream that is used to output all the error data that a
program might throw, on a computer screen or any standard output device.
This stream also uses all the 3 above-mentioned functions to output the error data:
print()
println()
printf()
Example:
Import java.io.*;
charc;
do{
c = (char)inp.read();
System.out.println(c);
} while(c != '0');
Input:
GeeksforGeeks0
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
0
Types of Streams:
Depending on the type of operations, streams can be divided into two primary classes:
Input Stream: These streams are used to read data that must be taken as an input from a source
array or file or any peripheral device. For eg., FileInputStream, BufferedInputStream,
ByteArrayInputStream etc.
Output Stream: These streams are used to write data as outputs into an array or file or any
output peripheral device. For eg., FileOutputStream, BufferedOutputStream,
ByteArrayOutputStream etc.
Depending on the types of file, Streams can be divided into two primary classes which can be
further divided into other classes as can be seen through the diagram below followed by the
explanations.
ByteStream: This is used to process data byte by byte (8 bits). Though it has many classes, the
FileInputStream and the FileOutputStream are the most popular ones. The FileInputStream is
used to read from the source and FileOutputStream is used to write to the destination. Here is the
list of various ByteStream Classes:
Stream class Description
PrintStream This contains the most used print() and println() method
DataOutputStream This contains method for writing java standard data types.
Import java.io.*;
try{
Int temp;
targetStream.write((byte)temp);
finally{
if(sourceStream != null)
sourceStream.close();
if(targetStream != null)
targetStream.close();
Output:
CharacterStream: In Java, characters are stored using Unicode conventions (Refer this for
details). Character stream automatically allows us to read/write data character by character.
Though it has many classes, the FileReader and the FileWriter are the most popular ones.
FileReader and FileWriter are character streams used to read from the source and write to the
destination respectively. Here is the list of various CharacterStream Classes:
PrintWriter This contains the most used print() and println() method
2. Example:
// and IOException
Import java.io.*;
try{
// character by character.
Int temp;
System.out.println((char)temp);
finally{
if(sourceStream != null)
sourceStream.close();
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 −
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 −
Once you have InputStream object in hand, then there is a list of helper methods which can
be used to read to stream or to do other operations on the stream.
Sr.N
Method & Description
o.
There are other important input streams available, for more detail you can refer to the
following links −
ByteArrayInputStream
DataInputStream
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 −
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 −
Once you have OutputStream object in hand, then there is a list of helper methods, which
can be used to write to stream or to do other operations on the stream.
Sr.N
Method & Description
o.
There are other important output streams available, for more detail you can refer to the
following links −
ByteArrayOutputStream
DataOutputStream
Example
import java.io.OutputStream;
The above code would create file test.txt and would write given numbers in binary format.
Same would be the output on the stdout screen.
BufferedInputStream and BufferedOutputStream are two classes in Java that are used to
improve the performance of input and output operations. They do this by buffering data,
which means that they store a small amount of data in memory before writing it to or
reading it from the underlying stream. This can improve performance because it reduces the
number of times that the underlying stream needs to be accessed.
BufferedInputStream and BufferedOutputStream can be used with any type of stream, but
they are especially useful for streams that are slow to access, such as network streams or
file streams.
BufferedInputStream and BufferedOutputStream are very useful classes for improving the
performance of input and output operations. They are easy to use and can be used with any
type of stream.
Here are some additional things to keep in mind when using BufferedInputStream and
BufferedOutputStream:
read(byte[] b, int off, Reads up to `len` bytes of data from this input stream into an
int len) array of bytes, starting at offset `off` in the array.
skip(long n) Skips over and discards `n` bytes of data from this input stream.
Flushes this output stream and forces any buffered output bytes
flush()
to be written out to the underlying device.
write(byte[] b) Writes `b.length` bytes from the specified byte array to this
Method Description
output stream.
write(byte[] b, int off, Writes `len` bytes from the specified byte array starting at offset
int len) `off` to this output stream.
Code for Writing and Reading Data from the buffer using BufferedInputStream and
BufferedOutputStream
package practiceproject;
import java.io.*;
import java.util.*;
public class democlass {
Code to read data from the buffer after skipping a byte using BufferedInputStream
package practiceproject;
import java.io.*;
import java.util.*;
public class democlass {
}
}