Java Input-Output package is introduced to perform all input and output operations.
I/O stream has some classes in java.io package and some classes in java.nio.file.
• Byte Streams
• Character Streams
• Buffered Streams
Java provides these two popular streams that are used in to perform I/O operations.
System.out(System output stream)
It is used to provide output of data. System.out is a outputstream through which you can write characters.
Functions that are used with system.out in java.
System.out.print- It is used to print the text on screen and moves the cursor to end of the line. And next text prints starts from here.
System.out.println- It is also used tp print the text on the screen and moves the cursor to the next line. And next text starts print from here.
System.in(System input stream)
It is used to provide input of data. It reads data from the source (keyboard).
When we type any data from keyboard than it will be read by system.in.
This stream is used to perform read- write operations on character type.
Classes that are used to to read character data-
• Reader: To read character streams.
• InputStreamReader: It is used to read byte stream and converts it to character stream.
• FileReader: It is used to read characters from a file.
• BufferedReader: It reduces the I/O operations in the file system because in one read() call method more date can be read from the file.
Classes that are used to write character data-
• Writer: To write the character streams.
• OutputStreamWriter: It is used to write character streams and also convert it to byte streams.
• FileWriter: It is used to write characters to the file.
• BufferedWriter: It can also reduces the I/O operations in the file system because in one write() call method more date can be write in the file.
This stream is used to perform read-write operations on byte data. In byte stream, we have to work with raw data e.g. characters, image, Unicode data.
Classes that are used to read the byte data:
• InputStream: To read the byte streams.
• FileInputStream: It is used to read bytes from a file.
• BufferedInputStream: As we saw in the character streams, this is a more efficient method than FileInputStream.
Classes that are used to write the byte data:
• OutputStream: To write byte streams.
• FileOutputStream: It is used to write raw bytes to the file.
• ByteOutputStream: As we saw in the character streams, this is a more efficient method than FileOutputStream.
Program to create a new file
In this program, we are creating a new file, If same name of file is already exist then it will give us error otherwise it will successfully created.
Program to copy a file into new file.
In this program, we are copy the already existing file into another file, it it does not exist it will shoe an exception otherwise it will create successfully.