How do I turn a String into a InputStreamReader in java?
How can I transform a String
value into an InputStreamReader
?
Asked by: Emily443 | Posted: 23-01-2022
Answer 1
ByteArrayInputStream also does the trick:
InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) );
Then convert to reader:
InputStreamReader reader = new InputStreamReader(is);
Answered by: Connie583 | Posted: 24-02-2022
Answer 2
I also found the apache commons IOUtils
class , so :
InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(myString));
Answered by: Brad775 | Posted: 24-02-2022
Answer 3
Does it have to be specifically an InputStreamReader? How about using StringReader?
Otherwise, you could use StringBufferInputStream, but it's deprecated because of character conversion issues (which is why you should prefer StringReader).
Answered by: Chelsea563 | Posted: 24-02-2022Answer 4
Same question as @Dan - why not StringReader ?
If it has to be InputStreamReader, then:
String charset = ...; // your charset
byte[] bytes = string.getBytes(charset);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais);
Answered by: Elian104 | Posted: 24-02-2022
Answer 5
Are you trying to get a) Reader
functionality out of InputStreamReader
, or b) InputStream
functionality out of InputStreamReader
? You won't get b). InputStreamReader
is not an InputStream
.
The purpose of InputStreamReader
is to take an InputStream
- a source of bytes - and decode the bytes to chars in the form of a Reader
. You already have your data as chars (your original String). Encoding your String into bytes and decoding the bytes back to chars would be a redundant operation.
If you are trying to get a Reader
out of your source, use StringReader
.
If you are trying to get an InputStream
(which only gives you bytes), use apache commons IOUtils.toInputStream(..)
as suggested by other answers here.
Answer 6
You can try Cactoos:
InputStream stream = new InputStreamOf(str);
Then, if you need a Reader
:
Reader reader = new ReaderOf(stream);
Answered by: Alissa858 | Posted: 24-02-2022
Similar questions
java - how to read using InputStreamReader without blocking
Is there a simple way to turn the standard code:
in = new InputStreamReader(socket.getInputStream());
String message = "";
while ((size = in.read(buf)) != -1){
message += new String(buf, 0, size);
}
so that read() won't block. I tried doing:
if (!in.ready())
//throw exception
but this throws an exception all the time. Maybe:
...
string - Read multiple lines from InputStreamReader (JAVA)
I have an InputStreamReader object. I want to read multiple lines into a buffer/array using one function call (without crating a mass of string objects). Is there a simple way to do so?
java - InputStreamReader buffering issue
I am reading data from a file that has, unfortunately, two types of character encoding.
There is a header and a body. The header is always in ASCII and defines the character set that the body is encoded in.
The header is not fixed length and must be run through a parser to determine its content/length.
The file may also be quite large so I need to avoid bring the entire content into memory.
java - setting a timeout for an InputStreamreader variable
I have a server running that accepts connections made through client sockets, I need to read the input from this client socket, now suppose the client opened a connection to my server without sending anything through the server's socket outputstream, in this case, while my server tried to read the input through the client socket's inputstream, an exception will be thrown, but before the exception is thrown i would like a ...
java - problem using base64 encoder and InputStreamReader
I have some CLOB columns in a database that I need to put Base64 encoded binary files in.
These files can be large, so I need to stream them, I can't read the whole thing in at once.
I'm using org.apache.commons.codec.binary.Base64InputStream to do the encoding, and I'm running into a problem. My code is essentially this
FileInputStream fis = new FileInputStream(file);
Base64InputStrea...
java - InputStream vs InputStreamReader
What's the benefit of using InputStream over InputStreamReader, or vice versa.
Here is an example of InputStream in action:
InputStream input = new FileInputStream("c:\\data\\input-text.txt");
int data = input.read();
while(data != -1) {
//do something with data...
doSomethingWithData(data);
data = input.read();
}
input.close();
And he...
Java InputStreamReader
Is there any simple and efficient way to duplicate an InputStreamReader?
InputStreamReader or Console for Java I/O
To get user input in Java which is the better method?
reader = new BufferedReader(new InputStreamReader(System.in));
or
by use of Console--> Console c = System.console();
what is the difference between the two and which one is to be used. Or is there any other better method other than these two?
java - bufferReader or InputStreamReader always throws exception
I created a java program that would just read a webpages code... google's for example and it works fine. However I have tried to implement the same code in an android application both in the emulator and on the actual device-Droid X.
I've tried two separate ways and have found either way it is the same line that throws the IOException. It is the line that creates the new bufferReader. I don't know if it is the b...
java - How to combine InputStreamReader with StringBuffer?
import java.io.*;
public class MyName
{
public static void main (String [] xxx) throws IOException
{
String a;
BufferedReader read = new BufferedReader (new InputStreamReader (System.in));
System.out.print("Input A Name : ");
a = read.readLine();
System.out.print("\n");
System.out.println("Your Name : " +a);
System.out.println("Name Capa...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)