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 Posts:
- What exactly is Apache Camel?
- How does the Java ‘for each’ loop work?
- How does the Java ‘for each’ loop work?
- What does “Could not find or load main class” mean?
- What is the proper way to handle a NumberFormatException when it is expected?
- How to convert/parse from String to char in java?
- Is there a Heap in java?
- Error parsing data org.json.JSONException: Value
- What is the difference between a static method and a non-static method?
- What is Java Servlet?
- Non-static variable cannot be referenced from a static context
- Round a double to 2 decimal places [duplicate]
- “Missing return statement” within if / for / while
- com.atomikos.icatch.SysException: Error in init: Log already in use? tmlog in ./
- exception in initializer error in java when using Netbeans
- Can I catch multiple Java exceptions in the same catch clause?
- Getting a HeadlessException: No X11 DISPLAY variable was set
- in java what does the @ symbol mean?
- Switch on Enum in Java
- How to call a method in another class in Java?
- error upon assigning Layout: BoxLayout can’t be shared
- Error: Could not find or load main class in intelliJ IDE
- How to connect to FTP over TLS/SSL (FTPS) server in Java
- How to check type of variable in Java?
- Convert .class to .java
- Recursive print Factorial
- What is Serialization?
- What is the best/simplest way to read in an XML file in Java application?
- Received fatal alert: handshake_failure through SSLHandshakeException
- Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?
- HashSet vs. ArrayList
- restart console on Minecraft Server
- SSL peer shut down incorrectly in Java
- Java split string to array
- Censored Words Condition
- Get all unique values in a JavaScript array (remove duplicates)
- Removing whitespace from strings in Java
- org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
- Generate a random double in a range
- How to get locator of current web element?
- Polymorphism vs Overriding vs Overloading
- javac: file not found: first.java Usage: javac
- java.net.SocketException: Connection reset
- (The Triangle class) Design a class named Triangle that extends GeometricObject
- Java – How to do floor division?
- Short form for Java if statement
- Access restriction: The type ‘Application’ is not API (restriction on required library rt.jar)
- Web server failed to start. Port 8080 was already in use. Spring Boot microservice
- local variables referenced from an inner class must be final or effectively final
- Java and Windows – error: illegal escape character
- Bank Account Java Program
- “Javac” doesn’t work correctly on Windows 10
- Eclipse error, “The selection cannot be launched, and there are no recent launches”
- RecyclerView onClick
- Failed to load the JNI shared Library (JDK)
- Java Error: Invalid top level statement
- IntelliJ – Can’t start – “JAVA_HOME does not point to a valid JVM installation”
- Syntax error on token(s), misplaced construct(s)
- Class is not Abstract and does not Override error in Java
- JAVA Variable declaration not allowed here
- Comparing strings by their alphabetical order
- exposed beyond app through ClipData.Item.getUri
- Corrupt jar file
- Java word count program
- How to write an ArrayList of Strings into a text file?
- MultipartException: Current request is not a multipart request
- difference between equals() and hashCode()
- The C# Shorthand getters and setters
- try catch ArrayIndexOutOfBoundsException?
- how to make jni.h be found?
- Cannot resolve symbol ‘button’ -Android Studio
- Syntax error on token “;”, { expected after this token in Random string creator
- Two dimensional array list
- Java: How To Call Non Static Method From Main Method?
- What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?
- How to “wait” a Thread in Android
- How to use regex in String.contains() method in Java
- Error: Module not specified (IntelliJ IDEA)
- Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
- What’s the difference between JPA and Hibernate?
- Throwing multiple exceptions in Java
- fixing errors on a program to call methods in java
- How can I calculate a time difference in Java?
- Any shortcut to initialize all array elements to zero?
- Creating a Calendar using javafx
- Align printf output in Java
- How to print binary tree diagram in Java?
- How to get rows and columns count of a 2D array in Java?
- Difference between volatile and synchronized in Java
- intellij idea – Error: java: invalid source release 1.9
- How fix Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- Is it okay to throw NullPointerException programmatically?
- Read next word in java
- Exception in thread “AWT-EventQueue-0” java.lang.OutOfMemoryError: Java heap space
- How to enable the Java keyword assert in Eclipse program-wise?
- return an ArrayList method
- Could not find or load main class with a Jar File
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- How to convert java.util.Date to java.sql.Date?
- How can I design a class named allergy?