InputStream

java.io.InputStream – InputStream Java Example

Photo of Nikos MaravitsasNikos MaravitsasApril 11th, 2014Last Updated: March 18th, 2021
0 364 15 minutes read

In this example, we are going to talk about a very important Javaclass, InputStream. If you have even the slightest experience with programming in Java, chances are that you’ve already usedInputStream in one of your programs, or one of its subclasses, likeFileInputStream orBufferedInputStream.

InputStream Java

You see, java io inputstream is an abstract class, that provides all the necessary API methods you can use in order to read data from a source. That source can be anything : a console, a file, a socket, a pipe, and even an array of bytes that resides on memory. The truth is that most of the times, when reading data from source, your program is actually reading a stream of bytes that resides on memory.

But what you need to know is that a concrete java io inputstream is connected to one of the aforementioned data resources. It’s main purpose is to read data from that source an make them available for manipulation from inside your program.

1. Simple InputStream Java Examples

Ok so let’s see a simple java io inputstream example on how you can read bytes form the console.

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.IOException;
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
 
        try{
            System.out.println("Available bytes :"+System.in.available());
            System.out.print("Write something :");
            intb = System.in.read();
            System.out.println("Available bytes :"+System.in.available());
            System.out.println("Input was :"+b);
        }catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

Let’s clear things up:System.in is anInputStream that is connected to the standard input. This means that its can be used to read data from the console. In this snippet we’ve used twoInputStream API methods :

  • read(): This methods reads the next byte from the input stream and returns it as an integer from 0 to 255. If theInputStream has no more data or if it is closed,read() will return -1.read() is a blocking IO method. This means that it either waits until the byte is read, or returns -1 if the stream has no more data or is closed. It also throws anIOException, that has to be handled.
  • available(): This method will return anestimation of the number of available bytes that you can read from theInputStream without blocking.

If you run the program, it willoutput:

Available bytes :0Write something :wepfokpd]asdfooefe02423-=VF2VWVVESAafAvailable bytes :38Input was :119

So as you can see when we initially callSystem.in.available() the available bytes to read are 0, so consequently we are going to block to the nextread() call. Then we type an arbitrary string. As you can see, this string consists of 39 bytes (included is the byte of ‘\n’, because you have to press “Return/Enter” to make the typed string available to theInputStream). When we callread() we just read the first byte of theInputStream, which is evaluated to 119 (read returns the byte an integer from 0 to 255). Then, you see that the output says 38 available bytes. Remember that we’ve already read one byte in the previous line, whenread() returned.

You can also choose to read a number of bytes in a byte array, instead of reading just one byte, To do that you can usepublic int read(byte[] b):

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.IOException;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
        byte[] bytes =newbyte[30];
 
        try{
            System.out.println("Available bytes :"+System.in.available());
            System.out.print("Write something :");
            intbytesread = System.in.read(bytes);
 
            System.out.println("I've read :"+bytesread +" bytes from the InputStream");
            System.out.println(Arrays.toString(bytes));
 
        }catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

If you run the program, it willoutput:

Available bytes :0Write something :sdjfsjdflksjdlfjksjdfI've read :23 bytes from the InputStream[115, 100, 106, 102, 115, 106, 100, 102, 108, 107, 115, 106, 100, 108, 102, 106, 107, 115, 106, 100, 102, 13, 10, 0, 0, 0, 0, 0, 0, 0]

As you can see I’ve read 23 bytes form the stream and placed it a byte array. An important thing to notice here is that while my byte array was 30 bytes log, it is not necessary that read will actually read 30 bytes. It will read as many bytes as possible, thus it will make an attempt to read up to 50 bytes, but it will actually read as many bytes are available up to 50. In this case, 23 bytes were available. The number of bytes it actually read is returned fromread() as an integer. If the the byte array is of length zero, then no bytes are read, andread() will return immediately ‘0’.

You can also choose to read a number of bytes and place them in an arbitrary position in you buffer array, instead of filling up your array. To do that you can usepublic int read(byte[] b, int off, int len), where inoff you specify the offset from the start of the buffer that you want to start placing the read bytes, andlen is the number of bytes you wish to read from the stream.

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.IOException;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
        byte[] bytes =newbyte[30];
 
        try{  
            System.out.println("Available bytes :"+System.in.available());
            System.out.print("Write something :");
            intbytesread = System.in.read(bytes,5,14);
 
            System.out.println("I've read :"+bytesread +" bytes from the InputStream");
            System.out.println(Arrays.toString(bytes));
 
        }catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

If you run the program, it willoutput:

Available bytes :0Write something :posjdpojsdpocjspojdcopsjcdpojspodcjpsjdocjpsdca[spdcI've read :14 bytes from the InputStream[0, 0, 0, 0, 0, 112, 111, 115, 106, 100, 112, 111, 106, 115, 100, 112, 111, 99, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

As you can see, the bytes we places from bytes[6] to bytes[19]. All other bytes are unaffected.

2. Reading characters form InputStream

When you’re dealing with binary data, it is usually fine to read bytes from the input stream. But, as you might agree, reading bytes is not always handy, especially when reading streams of characters, like we did in the example. For that, Java offers specialReader classes, that convert byte streams to character streams. It does that by simply parsing the bytes and encoding them according to character set encoding (you can do that on your own, but don’t even bother). Such aReader isInputStreamReader. To create anInputStreamReader, you give it anInputStream as an argument in its constructor, optionally along with a character set (or else the default will be used to encode the characters).

Let’s see how you can use it to read characters from the console:

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
        char[] characters =newchar[30];
 
        try{
            InputStreamReader inputReader =newInputStreamReader(System.in,"utf-8");
 
            System.out.print("Write some characters :");
 
            intbytesread = inputReader.read(characters);
 
            System.out.println("I've read :"+bytesread +" characters from the InputStreamReader");
            System.out.println(Arrays.toString(characters));
 
        }catch(IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }
}

If you run the program, it willoutput :

Write some characters :JavaCodeGeeksI've read :15 characters from the InputStreamReader[J, a, v, a, C, o, d, e, G, e, e, k, s, , , , , , , , , , , , , , , , , , ]

So as you can see, I can now read characters instead of bytes. Of coursepublic int read(char[] cbuf, int offset, int length) method is also available from theReader that offers the same basic functionality as we’ve described before in the case ofInpuStream. Same goes forread(), but instead of reading one bytes, it reads one character.

3. Using BufferedReader

You can also buffer aReader, mainly for efficiency. But, you can also take advantage of it when reading character streams, as you can pack characters inStrings. Thus, you can read a text input stream line by line.

Let’s see how :

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
 
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
 
        try{
            InputStreamReader inputReader =newInputStreamReader(System.in,"utf-8");
            BufferedReader buffReader =newBufferedReader(inputReader);
 
            System.out.print("Write a line :");
 
            String line = buffReader.readLine();
 
            System.out.println("Line read :"+line);
 
        }catch(IOException e) {
            e.printStackTrace();
        }  
    }
}

If you run the program, it willoutput:

Write a line :Java Code Geeks Rock !Line read :Java Code Geeks Rock !

You can still usepublic int read(char[] cbuf, int off, int len) methods to read characters to buffers, if you want. Buffered reader will efficiently read bytes, using an internal buffer. It appends the input in that buffer and performs its conversions there. The size of that internal buffer can be specified, if the default of 512 characters is not enough for you, using publicBufferedReader(Reader in, int sz) constructor, in thesz argument.

4. Read files using FileInputStream

FileInputStream is a sub class ofInputStream that is used to read files. Let’s see how you can use it :

InputStreamExample.java:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    privatestaticfinalString FILE_PATH="F:\\nikos7\\Desktop\\testFiles\\textFile.txt";
 
    publicstaticvoidmain(String[] args){
 
        InputStream fileInputStream =null;
        byte[] bytes =newbyte[20];
 
        try{
 
            fileInputStream =newFileInputStream(FILE_PATH);
 
            System.out.println("Available bytes of file:"+fileInputStream.available());
 
            intbytesread = fileInputStream.read(bytes,0,15);
 
            System.out.println("Bytes read :"+bytesread);
            System.out.println(Arrays.toString(bytes));
 
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
 
            try{
 
                fileInputStream.close();
 
            }catch(IOException e) {
 
                e.printStackTrace();
            }
        }  
    }
}

If you run the program, it willoutput:

Available bytes of file:173Bytes read :15[111, 112, 97, 112, 111, 115, 106, 99, 100, 111, 97, 115, 100, 118, 111, 0, 0, 0, 0, 0]

If you want to read buffered binary data, thus no need to use aReader, you can useBufferedInputStream.

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.BufferedInputStream;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    privatestaticfinalString FILE_PATH="F:\\nikos7\\Desktop\\testFiles\\textFile.txt";
 
    publicstaticvoidmain(String[] args){
 
        InputStream fileInputStream =null;
        BufferedInputStream bufferedInputStream =null;
        byte[] bytes =newbyte[512];
 
        try{
            fileInputStream =newFileInputStream(FILE_PATH);
            bufferedInputStream =newBufferedInputStream(fileInputStream,1024);
 
            intbytesread = bufferedInputStream.read(bytes,0,512);
 
            System.out.println("Bytes read :"+bytesread);
            System.out.println(Arrays.toString(bytes));
 
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
 
            try{
 
                bufferedInputStream.close();
 
            }catch(IOException e) {
 
                e.printStackTrace();
            }
        }  
    }
}

As you can see here, we’ve specified the internal buffer to be 1024 bytes.

If you run the program, it willoutput:

Bytes read :173[111, 112, 97, 112, 111, 115, 106, 99, 100, 111, 97, 115, 100, 118, 111, 112, 97, 115, 100, 118, 13, 10, 97, 115, 100, 118, 111, 112, 97, 115, 111, 100, 106, 118, 111, 112, 106, 97, 112, 115, 111, 118, 91, 97, 115, 100, 118, 13, 10, 112, 111, 97, 115, 100, 118, 112, 111, 106, 97, 115, 100, 118, 91, 97, 115, 107, 100, 118, 91, 112, 107, 91, 13, 10, 115, 97, 100, 118, 112, 115, 111, 106, 100, 118, 111, 106, 115, 112, 111, 100, 118, 106, 13, 10, 115, 100, 118, 111, 106, 112, 111, 106, 118, 112, 97, 111, 115, 106, 100, 112, 118, 106, 112, 111, 97, 115, 106, 100, 118, 13, 10, 97, 115, 106, 100, 118, 111, 106, 112, 97, 111, 115, 106, 100, 112, 118, 106, 112, 97, 111, 115, 106, 100, 118, 97, 115, 100, 118, 13, 10, 97, 111, 115, 100, 98, 102, 112, 106, 97, 111, 115, 106, 100, 111, 98, 106, 97, 115, 112, 111, 100, 98, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Of course you can use all the above methods to bridge the byte stream to a character streams. So let’s see how you can read a text file,line by line

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
 
publicclassInputStreamExample {
 
    privatestaticfinalString FILE_PATH="F:\\nikos7\\Desktop\\testFiles\\textFile.txt";
 
    publicstaticvoidmain(String[] args){
 
        InputStream fileInputStream =null;
        BufferedReader bufferedReader =null;
 
        try{
 
            fileInputStream =newFileInputStream(FILE_PATH);
            bufferedReader =newBufferedReader(newInputStreamReader(fileInputStream));
 
            String line="";
            while( (line = bufferedReader.readLine()) !=null){
                System.out.println(line);
            }
 
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
 
            try{
 
                bufferedReader.close();
 
            }catch(IOException e) {
 
                e.printStackTrace();
            }
        }  
    }
}

If you run the program, it willoutput:

opaposjcdoasdvopasdvasdvopasodjvopjapsoveasdvpoasdvpojasdvwaskdvepkesadvpsojdvojspodvjsdvojpojvpaosjdpvjpoasjdvasjdvojpaosjdpvjpaosjdvasdvaosdbfpjaosjdobjaspodbj

5. Read data from memory

To read data form memory, you can use a different subclass ofInputStream,ByteArrayInputStream.

Let’s see how you can use it:

InputStreamExample.java:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.BufferedReader;
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.Arrays;
 
publicclassInputStreamExample {
 
    publicstaticvoidmain(String[] args){
 
        String str1 ="aosdjfopsdjpojsdovjpsojdvpjspdvjpsjdv";
        String str2 ="aosdjfopsdjpojsdovjpsojdvpjspdvjpsjdv \n"
                +"sidjvijsidjvisjdvjiosjdvijsiodjv \n"
                +"ajsicjoaijscijaisjciajscijaiosjco \n"
                +"asicoaisjciajscijascjiajcsioajsicjioasico";
 
        byte[] bytes =newbyte[512];
 
        InputStream inputStream =newByteArrayInputStream(str1.getBytes());
        BufferedReader bufReader =new BufferedReader(newInputStreamReader (newByteArrayInputStream(str2.getBytes())));
 
        try{
 
            intbytesread = inputStream.read(bytes,0,50);
            System.out.println("Bytes read from str1 :"+bytesread);
            System.out.println("Bytes read from str1 :"+Arrays.toString(bytes));
 
            String line ="";
            while( (line = bufReader.readLine()) !=null){
                System.out.println("Line of str1 :"+ line);
            }
 
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
            try{
                bufReader.close();
                inputStream.close();
            }catch(IOException e) {
 
                e.printStackTrace();
            }
        }
    }
}

If you run the program, it willoutput:

Bytes read from str1 :37Bytes read from str1 :[97, 111, 115, 100, 106, 102, 111, 112, 115, 100, 106, 112, 111, 106, 115, 100, 111, 118, 106, 112, 115, 111, 106, 100, 118, 112, 106, 115, 112, 100, 118, 106, 112, 115, 106, 100, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]Line of str1 :aosdjfopsdjpojsdovjpsojdvpjspdvjpsjdv Line of str1 :sidjvijsidjvisjdvjiosjdvijsiodjv Line of str1 :ajsicjoaijscijaisjciajscijaiosjco Line of str1 :asicoaisjciajscijascjiajcsioajsicjioasico

6. Reading from other sources

As you might image an sublclass ofInputStream is present to help you read from all aforementioned sources. Most IO classes have an interface method that enables you to obtain anInputStream connected to a particular source. From then on, you can use all the above methods to enable buffering, or to simply bridge a byte stream to a character stream.

7. Mark and Reset

Java ioInputStream offers to methodsmark andreset.mark is used to place a marker in the current position of the stream. You can think of it as apointer to the present point of the stream. As you scan though the stream you can place markers at arbitrary points. After a while, if you call reset, the “cursor” of the stream will go to the last marker you’ve placed, so you can re-read the same data, particularly useful to perform error correction e.t.c.

Let’s see an example here withBufferedReader. Using them withInputStream and its subclasses is the exact same thing.

InputStreamExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
packagecom.javacodegeeks.core.io.inputstream;
 
importjava.io.BufferedReader;
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Arrays;
 
publicclassInputStreamExample {
     
    publicstaticvoidmain(String[] args){
 
        String str1 ="Java Code Geeks Rock!";
         
        char[] cbuf =newchar[10];
         
         
        BufferedReader bufReader =new BufferedReader(newInputStreamReader (newByteArrayInputStream(str1.getBytes())));
         
        try{
         
            intcharsread = bufReader.read(cbuf,0,5);
            System.out.println(Arrays.toString(cbuf));
             
            bufReader.mark(120);
             
            charsread = bufReader.read(cbuf,0,5);
            System.out.println(Arrays.toString(cbuf));
             
            bufReader.reset();
             
            charsread = bufReader.read(cbuf,0,5);
            System.out.println(Arrays.toString(cbuf));
             
             
        }catch(IOException e) {
            e.printStackTrace();
        }finally{
            try{
                bufReader.close();
         
            }catch(IOException e) {
             
                e.printStackTrace();
            }
        }
    }
}

The integer “120” argument you see in themark method is a threshold for the maximum number of bytes that can be read before releasing the marker. So, here if we read more than 120 characters the marker will be removed.

If you run the program, it willoutput:

[J, a, v, a,  ,  ,  ,  , ] [C, o, d, e,  ,  ,  ,  , ] [C, o, d, e,  ,  ,  ,  , ]

8. Download Source Code

This was a java.io.InputStream Example.

Download
You can download the source code of this example here:java.io.InputStream – InputStream Java Example

Last updated on May 28th, 2020

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nikos MaravitsasNikos MaravitsasApril 11th, 2014Last Updated: March 18th, 2021
0 364 15 minutes read
Photo of Nikos Maravitsas

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.