Example of ByteArrayInputStream class in java.

Example of ByteArrayInputStream class in java.


Posted in : Core Java Posted on : October 21, 2010 at 12:52 PM Comments : [ 0 ]

In this example, we will see how to convert InputStream into ByteArrayInputStream in java.

Example of ByteArrayInputStream class in java.

In this example, you will see how to convert InputStream into ByteArrayInputStream in java. First of all read the file as input stream using FileInputStream. Then convert this input stream into string by using the toString() and string into byte by using getBytes() methods. This array works as source for ByteArrayInputStream constructor.

Code:
ByteArrayInputStreamDemo.java
package devmanuals.com;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ByteArrayInputStreamDemo {
	public static void main(String[] args) throws IOException {

	FileInputStream inputStream = new FileInputStream("TestFile.txt");
	byte[] byteArray = inputStream.toString().getBytes();
	ByteArrayInputStream byteArrayInStream = new ByteArrayInputStream(
			byteArray);
	while (byteArrayInStream.read() != -1) {
		System.out.print(byteArrayInStream.read());
	}
   }
}
Output:
97
97
105
46
105
101
110
117
83
114
97
64
57
50
102

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics