Get an OutputStream into a String
I would use a ByteArrayOutputStream
. And on finish you can call:
new String( baos.toByteArray(), codepage );
or better:
baos.toString( codepage );
For the String
constructor, the codepage
can be a String
or an instance of java.nio.charset.Charset. A possible value is java.nio.charset.StandardCharsets.UTF_8.
The method toString()
accepts only a String
as a codepage
parameter (stand Java 8).
How to convert an OutputStream into a string?
InputStream
and OutputStream
are for byte sequences. Reader
and Writer
are for character sequences, like String
s.
To turn an OutputStream
into a Writer
, do new OutputStreamWriter(outputStream)
, or much better, use new OutputStreamWriter(outputStream, Charset)
to specify a Charset
, which describes a way of converting between characters and bytes.
(The other direction, InputStreamReader
, is similar.)
I want to convert an output stream into String object
not very familiar with jaxb, from what i was able to find you can convert into a string using
public String asString(JAXBContext pContext,
Object pObject)
throws
JAXBException {
java.io.StringWriter sw = new StringWriter();
Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(pObject, sw);
return sw.toString();
}
ws.apache.org
but I'm not sure about a stirng object. still searching.
** EDIT
Marshalling a non-element
Another common use case is where you
have an object that doesn't have
@XmlRootElement on it. JAXB allows you
to marshal it like this:marshaller.marshal( new JAXBElement(
new
QName("","rootTag"),Point.class,new
Point(...)));This puts the element as the
root element, followed by the contents
of the object, then . You
can actually use it with a class that
has @XmlRootElement, and that simply
renames the root element name.At the first glance the second
Point.class parameter may look
redundant, but it's actually necessary
to determine if the marshaller will
produce (infamous) @xsi:type. In this
example, both the class and the
instance are Point, so you won't see
@xsi:type. But if they are different,
you'll see it.This can be also used to marshal a
simple object, like String or an
integer.marshaller.marshal( new JAXBElement(
new
QName("","rootTag"),String.class,"foo
bar"));But unfortunately it cannot be used to
marshal objects like List or Map, as
they aren't handled as the first-class
citizen in the JAXB world.
found HERE
OutputStream not converting into a String varible
Looks like i finally got it!
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(istrm_));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
Write string to output stream
Streams (InputStream
and OutputStream
) transfer binary data. If you want to write a string to a stream, you must first convert it to bytes, or in other words encode it. You can do that manually (as you suggest) using the String.getBytes(Charset)
method, but you should avoid the String.getBytes()
method, because that uses the default encoding of the JVM, which can't be reliably predicted in a portable way.
The usual way to write character data to a stream, though, is to wrap the stream in a Writer
, (often a PrintWriter
), that does the conversion for you when you call its write(String)
(or print(String)
) method. The corresponding wrapper for InputStreams is a Reader.
PrintStream
is a special OutputStream
implementation in the sense that it also contain methods that automatically encode strings (it uses a writer internally). But it is still a stream. You can safely wrap your stream with a writer no matter if it is a PrintStream
or some other stream implementation. There is no danger of double encoding.
Example of PrintWriter with OutputStream:
try (PrintWriter p = new PrintWriter(new FileOutputStream("output-text.txt", true))) {
p.println("Hello");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
Get an OutputStream into a String
I would use a ByteArrayOutputStream
. And on finish you can call:
new String( baos.toByteArray(), codepage );
or better:
baos.toString( codepage );
For the String
constructor, the codepage
can be a String
or an instance of java.nio.charset.Charset. A possible value is java.nio.charset.StandardCharsets.UTF_8.
The method toString()
accepts only a String
as a codepage
parameter (stand Java 8).
How do I read / convert an InputStream into a String in Java?
A nice way to do this is using Apache commons IOUtils
to copy the InputStream
into a StringWriter
... something like
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
or even
// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding);
Alternatively, you could use ByteArrayOutputStream
if you don't want to mix your Streams and Writers
Related Topics
Get Maven Artifact Version at Runtime
Java Recursive Fibonacci Sequence
Where Is the Documentation for the Values() Method of Enum
Converting Secret Key into a String and Vice Versa
Named Placeholders in String Formatting
Java Synchronized Block for .Class
Mockito:How to Verify Method Was Called on an Object Created Within a Method
What Does It Mean: the Serializable Class Does Not Declare a Static Final Serialversionuid Field
How to Call a Method with a Separate Thread in Java
Adding Up Bigdecimals Using Streams
Differencebetween Class.This and This in Java
A Regex to Match a Substring That Isn't Followed by a Certain Other Substring
How to Get the File Name from a String Containing the Absolute File Path
Maven: Failed to Read Artifact Descriptor
Functional Style of Java 8's Optional.Ifpresent and If-Not-Present
How to Make Notepad to Save Text in Utf-8 Without the Bom
How to Change the Dock Icon of a Java Program
Using Powermockito.Whennew() Is Not Getting Mocked and Original Method Is Called