Java Create an InputStream

Java Create an InputStream

To create an InputStream in Java, you can use one of the classes that implements the InputStream interface, such as ByteArrayInputStream, FileInputStream, or SocketInputStream.

For example, to create a ByteArrayInputStream, you can use the following code:

byte[] data = {1, 2, 3, 4, 5};
InputStream stream = new ByteArrayInputStream(data);
Sourc‮w:e‬ww.lautturi.com

This creates a new ByteArrayInputStream called stream that reads from the byte array data. The ByteArrayInputStream is a class that allows you to read a byte array as if it were an input stream.

To create a FileInputStream, you can use the following code:

InputStream stream = new FileInputStream("input.txt");

This creates a new FileInputStream called stream that reads from the file "input.txt". The FileInputStream is a class that allows you to read a file as if it were an input stream.

It is important to remember to close the InputStream when you are finished reading from it, to release any resources that it is using. You can do this using the close method, like this:

stream.close();
Created Time:2017-11-03 00:14:39  Author:lautturi