keyboard_arrow_up

The Best Way To Know about Java input and output stream?


Java I/O (Input and Output) is used to process the feedback and produce the outcome based on the feedback.




Java uses the idea of circulation to make I/O function fast. The java.io package contains all the sessions required for feedback and outcome functions.




file handling in java can be performed by java IO API.




Stream




A stream is a series of information. In Java a stream is comprised of bytes. Like a water flow stream; it is called a stream as it continues with the flow .




In java, 3 sources are created for you instantly. All these sources are connected with system.




1) Program.out: conventional output stream




2) Program.in: conventional input stream




3) Program.err: conventional error stream






OutputStream




Java program uses an outcome circulation to write information to a location, it may be a knowledge file,an range,peripheral system or outlet.




InputStream




Java program uses a port circulation to read information from a source, it may be a knowledge file,an range,peripheral system or outlet.




OutputStream class




OutputStream class is a subjective category.It is the superclass of all sessions comprising an outcome stream of bytes. An output stream allows output bytes and delivers them to some sink.




1) public void write(int)throws IOException: current output stream will contain byte due to this code




2) public void write(byte[])throws IOException: current output stream will contain an array of byte due to this code




3) public void flush()throws IOException: clears out the current output stream




4) public void close()throws IOException: closure of the current output stream




InputStream class




InputStream class is an subjective category. It is the superclass of all sessions comprising a port stream of bytes.




1) public abstract int read()throws IOException: returns -1 at the end of the file and it reads the next byte of data from the input stream.




2) public int available()throws IOException: number of readable bytes from the current input stream can be estimated




3) public void close()throws IOException: closure of the current input stream






Java FileOutputStream class




Java FileOutputStream is an outcome flow for composing information to a data file.




If you have to create basic principles then use FileOutputStream.Instead, for character-oriented information, choose FileWriter.But you can create byte-oriented as well as character-oriented information.




Example of Java FileOutputStream class


  1. import java.io.*;  

  2. class Test{  

  3.   public static void main(String args[]){  

  4.    try{  

  5.      FileOutputstream fout=new FileOutputStream("abc.txt");  

  6.      String s="Sachin Tendulkar is my favourite player";  

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






Java FileInputStream class




Java FileInputStream category acquires feedback bytes from a data file.It is used for studying sources of raw bytes such as picture information. For studying sources of figures, consider using FileReader.




It should be used to study byte-oriented information for example to read picture, sound, movie etc.




Example of FileInputStream class


  1. import java.io.*;  

  2. class SimpleRead{  

  3.  public static void main(String args[]){  

  4.   try{  

  5.     FileInputStream fin=new FileInputStream("abc.txt");  

  6.     int i=0;  

  7.     while((i=fin.read())!=-1){  

  8.      System.out.println((char)i);  

  9.     }  

  10.     fin.close();  

  11.   }catch(Exception e){system.out.println(e);}  

  12.  }  

  13. }  




Example of Reading the information of current Java information file and writing it into another file




We can read the information of information file using the FileInputStream class whether it is Java information file, image information file, video information file etc. In this example, we are reading the information of C.java information file and writing it into another information file M.java.




import java.io.*;  


  1. class C{  

  2. public static void main(String args[])throws Exception{  

  3. FileInputStream fin=new FileInputStream("C.java");  

  4. FileOutputStream fout=new FileOutputStream("M.java");  

  5. int i=0;  

  6. while((i=fin.read())!=-1){  

  7. fout.write((byte)i);  

  8. }  

  9. fin.close();  

  10. }  

  11. }  




You can join the training institute in Java to make your profession in this field.