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

Page 1

What is 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.

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.


Example of Java FileOutputStream class ●

import java.io.*;

class Test{ public static void main(String args[]){

try{

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

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

byte b[]=s.getBytes();//converting string into byte array

fout.write(b);

fout.close();

System.out.println("success..."); }catch(Exception e){system.out.println(e);}

}

}


Example of FileInputStream class ●

import java.io.*;

class SimpleRead{

public static void main(String args[]){ try{

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

int i=0;

while((i=fin.read())!=-1){ System.out.println((char)i);

}

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

} }


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.