Java, like many modern programming languages, supports the implementation of data input streams. In Java, an InputStream is the base class for byte streams. This class is abstract, that is, in the process of the program, we cannot create an instance of it. However, the io package has many classes that inherit and extend the functionality of InputStream. To use this class in your code, you need to import it from the java.io.InputStream package. Next, we look at the basic functionality that the InputStream class provides, as well as the main classes that implement it.
Methods of the InputStream Class
InputStream, , Closeable AutoCloseable. Closeable , . close(). InputStream IOException, try, close() finally, try.
AutoCloseable , close() finally. Java, InputStream try , .
InputStream:
- int available() – ;
- int read() – ; , -1;
- int read(byte[] ) – byte ( ; , ; , -1);
- int read(byte[] , int , int ) – , , «» , « »;
- long skip(long ) – .
InputStream , . .
FileInputStream. , , . , FileNotFoundException.
, , FileInputStream InputStream.
, . , , ? Java InputStream DataInputStream. InputStream, . .
DataInputStream DataInput . .
– .
BufferedInputStream Java. InputStream . , , . . BufferedInputStream InputStream , , .
:
- BufferedInputStream(InputStream );
- BufferedInputStream(InputStream , int )
, , .
We examined the main implementations of InputStream. If you need to convert the received bytes to a string, then in Java InputStream to String (), unfortunately, is not overloaded, so you will have to use special utilities, for example, IOUtils from the Apache Commons library.