Example:
package com.w3schools;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class ByteArrayToInputStream {
public static void main(String args[]){
String str = "Test Content";
byte[] content = str.getBytes();
int size = content.length;
InputStream is = null;
byte[] b = new byte[size];
try {
is = new ByteArrayInputStream(content);
is.read(b);
System.out.println("String content: "+new String(b));
} catch (Exception e) {
e.printStackTrace();
}
}
} |
package com.w3schools;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class ByteArrayToInputStream {
public static void main(String args[]){
String str = "Test Content";
byte[] content = str.getBytes();
int size = content.length;
InputStream is = null;
byte[] b = new byte[size];
try {
is = new ByteArrayInputStream(content);
is.read(b);
System.out.println("String content: "+new String(b));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
String content: Test Content |
String content: Test Content
Download this example.