How does one create an InputStream from a String? [duplicate] - java

This question already has answers here:
How do I turn a String into a InputStreamReader in java?
(6 answers)
Closed 6 years ago.
I'm not used to working with streams in Java - how do I create an InputStream from a String?

Here you go:
InputStream is = new ByteArrayInputStream( myString.getBytes() );
Update For multi-byte support use (thanks to Aaron Waibel's comment):
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString).array());
Please see ByteArrayInputStream manual.
It is safe to use a charset argument in String#getBytes(charset) method above.
After JDK 7+ you can use
java.nio.charset.StandardCharsets.UTF_16
instead of hardcoded encoding string:
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(myString).array());

You could do this:
InputStream in = new ByteArrayInputStream(string.getBytes("UTF-8"));
Note the UTF-8 encoding. You should specify the character set that you want the bytes encoded into. It's common to choose UTF-8 if you don't specifically need anything else. Otherwise if you select nothing you'll get the default encoding that can vary between systems. From the JavaDoc:
The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

InputStream in = new ByteArrayInputStream(yourstring.getBytes());

Java 7+
It's possible to take advantage of the StandardCharsets JDK class:
String str=...
InputStream is = new ByteArrayInputStream(StandardCharsets.UTF_16.encode(str).array());

Beginning with Java 7, you can use the following idiom:
String someString = "...";
InputStream is = new ByteArrayInputStream( someString.getBytes(StandardCharsets.UTF_8) );

Instead of CharSet.forName, using com.google.common.base.Charsets from Google's Guava (http://code.google.com/p/guava-libraries/wiki/StringsExplained#Charsets) is is slightly nicer:
InputStream is = new ByteArrayInputStream( myString.getBytes(Charsets.UTF_8) );
Which CharSet you use depends entirely on what you're going to do with the InputStream, of course.

Related

Create a FileInputStream from a String instead of a file in Java [duplicate]

Given a string:
String exampleString = "example";
How do I convert it to an InputStream?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".
I find that using Apache Commons IO makes my life much easier.
String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");
You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.
You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.
There are two ways we can convert String to InputStream in Java,
Using ByteArrayInputStream
Example :-
String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
Using Apache Commons IO
Example:-
String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
You can try cactoos for that.
final InputStream input = new InputStreamOf("example");
The object is created with new and not a static method for a reason.

Loading an ontology from string using OWL API [duplicate]

Given a string:
String exampleString = "example";
How do I convert it to an InputStream?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".
I find that using Apache Commons IO makes my life much easier.
String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");
You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.
You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.
There are two ways we can convert String to InputStream in Java,
Using ByteArrayInputStream
Example :-
String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
Using Apache Commons IO
Example:-
String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
You can try cactoos for that.
final InputStream input = new InputStreamOf("example");
The object is created with new and not a static method for a reason.

How to encode to windows 1252 in Java while writing to the file?

If I were to encode the data in windows-1252 format and write it to the file, how to set the content type in java?
You can use an OutputStreamWriter.
Writer out = new OutputStreamWriter(new FileOutputStream(yourFile), "windows-1252");
Use the typical Writer methods to write output to your File (or wrap it, or declare it as OutputStreamWriter).
The constructor also accepts a Charset instead of the String charset name. You can get it like so
Charset windows1252 = Charset.forName("windows-1252");
You need to specify the encoding. According to the documentation, you should use "Cp1252" when using java.io and java.lang classes and you should use "windows-1252" when using the java.nio classes.
So, for instance, you can do this:
File file = . . .;
Writer output = new PrintWriter(file, "Cp1252");
or, with java.nio, this:
File file = . . .;
Writer output = Files.newBufferedWriter(file.toPath(), "windows-1252");
(As Sotirios points out in a comment, you can probably use "windows-1252" throughout, despite what the docs say.)

BufferedReader returns ISO-8859-15 String - how to convert to UTF16 String?

I have an FTP client class which returns InputStream pointing the file. I would like to read the file row by row with BufferedReader. The issue is, that the client returns the file in binary mode, and the file has ISO-8859-15 encoding.
If the file/stream/whatever really contains ISO-8859-15 encoded text, you just need to specify that when you create the InputStreamReader:
BufferedReader br = new BufferedReader(
new InputStreamReader(ftp.getInputStream(), "ISO-8859-15"));
Then readLine() will create valid Strings in Java's native encoding (which is UTF-16, not UTF-8).
Try this:
BufferedReader br = new BufferedReader(
new InputStreamReader(
ftp.getInputStream(),
Charset.forName("ISO-8859-15")
)
);
String row = br.readLine();
The original string is in ISO-8859-15, so the byte stream read by your InputStreamReader will be in this encoding. So read in using that encoding (specify this in the InputStreamReader constructor). That tells the InputStreamReader that the incoming byte stream is in ISO-8859-15 and to perform the appropriate byte-to-character conversions.
Now it will be in the standard Java UTF-16 format, and you can then do what you wish.
I think the current problem is that you're reading it using your default encoding (by not specifying an encoding in InputStreamReader), and then trying to convert it, by which time it's too late.
Using default behaviour for these sort of classes often ends in grief. It's a good idea to specify encodings wherever you can, and/or default the VM encoding via -Dfile.encoding
Have you tried:
BufferedReader r = new BufferedReader(new InputStreamReader("ISO-8859-1"))
...

How do I convert a String to an InputStream in Java?

Given a string:
String exampleString = "example";
How do I convert it to an InputStream?
Like this:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".
I find that using Apache Commons IO makes my life much easier.
String source = "This is the source of my input stream";
InputStream in = org.apache.commons.io.IOUtils.toInputStream(source, "UTF-8");
You may find that the library also offer many other shortcuts to commonly done tasks that you may be able to use in your project.
You could use a StringReader and convert the reader to an input stream using the solution in this other stackoverflow post.
There are two ways we can convert String to InputStream in Java,
Using ByteArrayInputStream
Example :-
String str = "String contents";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
Using Apache Commons IO
Example:-
String str = "String contents"
InputStream is = IOUtils.toInputStream(str, StandardCharsets.UTF_8);
You can try cactoos for that.
final InputStream input = new InputStreamOf("example");
The object is created with new and not a static method for a reason.

Categories