info@tutsfinder.in



Java Input/Output

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.

I/O Stream:

• 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.


Charcter Stream-

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.


Byte Stream-

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.



  Note:- All these classes are defined under java.io package.
	


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.



import java.io.File;
import java.io.IOException;

public class CreateFile
{
   public static void main( String[] args )
   {	
      try {
	     File file = new File("C:\\JavaPrograms\\tutsfinder.txt");
             boolean check = file.createNewFile();
	     if (check){
	          System.out.println("File has been created successfully");
	     }
	     else{
	          System.out.println("File already exist !");
	     }
    	} catch (IOException e) {
    		System.out.println("Exception Occured");
	        e.printStackTrace();
	  }
   }
}


Output:

File has been created successfully


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.



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class CopyFile 
{
    public static void main(String[] args)
    {	

 
    	try{
    	    File infile =new File("C:\\JavaPrograms\\Tutsfinder.txt");
    	    File outfile =new File("C:\\JavaPrograms\\CopyTustfinder.txt");
 
    	    FileInputStream fis = new FileInputStream(infile);
    	    FileOutputStream fos = new FileOutputStream(outfile);
 
    	    byte[] buffer = new byte[1024];
 
    	    int length;
    	    
    	    while ((length = fis.read(buffer)) > 0){
    	    	fos.write(buffer, 0, length);
    	    }

    	    fis.close();
    	    fos.close();

    	    System.out.println("File successfully copied !");
 
    	}catch(IOException ioe){
    		System.out.println(ioe);
    	 }
    }
}


Output:

File successfully copied !