Input Stream
1. Byte oriented some common classes
i. Input stream:- This class is abstract. A division of the InputStream class is the System class. The input stream is the superclass of the input stream of bytes—examples of subclasses FileInputSttream, PipedInputStream, FilterInputStream, etc. InputStream class some common methods like
-
public abstract int read() //this is used to read the byte from the input stream
-
public long skip(long)`// skip n bytes and remove them from the input stream.
-
public int available() //provide an estimate of the number of bytes in the input stream
-
public void close() // close the input stream
Some common classes example:
a. Reading byte from byte array
byte x[]={20,21,22,23,24,25};
ByteArrayInputStream y=new ByteArrayInputStream(x);
y.read();
b. reading byte from a file
FileInputStream f=new FileInputStream(a.txt);
f.read();
c. Buffered byte from the keyboard
BufferedInputStream x=new BufferedInputStream(System.in);
x.read();
d. reading and writing both simultaneously (basically used for thread)
PipedInputStream p1=new PipedInputStream(PipedOutputStream);
p1.read();
Code Example
import java.io.*;
class Test {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Enter a number: ");
byte b = Byte.parseByte(br.readLine()); // reading byte input
System.out.println("You entered: " + b);
}
}
RANREV INFOTECH