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,580 points
182 views

2 answers 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,210 points
0 votes

Apache Commons allows:

String myString = IOUtils.toString(myInputStream, "UTF-8");

And you could of course choose other character encodings besides UTF-8

answered Aug 30, 2019 by Karan
• 18,440 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,110 points
1,548 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 58,130 points
1,475 views
0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 12 in Java by Roshni
• 4,690 points
505 views
0 votes
1 answer

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

Hello @kartik, We can use the parseInt(String str) method of ...READ MORE

answered Jul 28 in Java by Niroj
• 66,940 points
70 views
0 votes
1 answer

How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28 in Java by Niroj
• 66,940 points
91 views
0 votes
2 answers

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

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 58,130 points
533 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,890 points
10,346 views
0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 58,130 points
296 views
0 votes
2 answers

Result of character addition in Java

Binary arithmetic operations on char and byte ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 58,130 points
575 views
0 votes
2 answers

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

The best possible way of conversion between byte[] and String is to ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 58,130 points
1,129 views