Reading an InputStream & coverting it into a String in Java?

0 votes

Suppose I have a java.io.InputStream object, how can I process it to produce a String?
Let's sat my InputStream has text data, which I need to convert into a String so that I can write it to a log file.
Can someone tell me an easy way to either read the InputStream as a String or convert it into one?

public String convertStreamToString(InputStream is) { ...... }
Oct 8, 2018 in Java by Bharani
• 4,550 points
47 views

1 answer to this question.

0 votes

Use Apache commons IOUtils and copy the InputStream into a StringWriter like this:

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

or else

// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding); 

Also, you could use ByteArrayOutputStream if you don't want to mix your Streams and Writers

answered Oct 8, 2018 by DataKing99
• 8,100 points

Related Questions In Java

0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,040 points
105 views
0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,600 points
218 views
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
84 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,040 points
557 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
67 views
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
113 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,840 points
2,317 views
0 votes
1 answer

Get all the permutations of a string in Java

This is the recursive way of finding a ...READ MORE

answered May 10, 2018 in Java by sharth
• 3,320 points
71 views
0 votes
1 answer

Result of character addition in Java

The result of adding Java chars, shorts, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,540 points
41 views
0 votes
1 answer

How to convert byte array to String and STring to byte array?

The conversion between byte[] and String is to explicitly state the ...READ MORE

answered May 14, 2018 in Java by parth295
• 4,600 points
38 views