Convert InputStream to String in Java
π Converting InputStream to String in Java - The Fun Way! πβ
Ever found yourself wrestling with an InputStream
and thinking, "I wish this was a String already!"? Well, fret no more! Today, we're diving into multiple ways to convert an InputStream
into a String
like a Java ninja. π₯·π»
1οΈβ£ Using InputStream.readAllBytes()
(Since Java 9) π₯β
If you're using Java 9+, you're in luck! The readAllBytes()
method turns your stream into a byte array faster than you can say "Stream!" ππ¨
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
String fileContent = new String(in.readAllBytes());
β οΈ Warning: Avoid using this for files larger than 2GB unless you enjoy OutOfMemoryErrors! π¨
2οΈβ£ Using BufferedReader
- The Old Reliable πβ
Ah, BufferedReader
... The grandparent of Java file reading. Super efficient and still going strong! ποΈββοΈ
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
String fileContent = out.toString();
reader.close();
Or, if you're feeling fancy with Java 8, go for this sleek, stream-based approach: π
InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
String newLine = System.getProperty("line.separator");
String fileContent;
try (Stream<String> lines = new BufferedReader(new InputStreamReader(in)).lines()) {
fileContent = lines.collect(Collectors.joining(newLine));
}
3οΈβ£ Google Guava IO - The Modern Magic β¨β
If you love Google's open-source tools, Guava has some great utilities! π
3.1 Dependencies π¦β
Add this to your pom.xml
:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
3.2 Using ByteSource
πβ
InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return inputStream;
}
};
String fileContent = byteSource.asCharSource(Charsets.UTF_8).read();
3.3 Using CharStreams
πβ
InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));
String fileContent;
try (final Reader reader = new InputStreamReader(inputStream)) {
fileContent = CharStreams.toString(reader);
}
System.out.println(fileContent);
4οΈβ£ Apache Commons IO - The Power Tool β‘β
If your project already has Apache Commons IO, why not use its magic? πͺ
4.1 Dependencies π¦β
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
4.2 Using IOUtils
π οΈβ
Two ways to make it work like a charm! β¨
// Method 1: IOUtils.copy()
StringWriter writer = new StringWriter();
IOUtils.copy(new FileInputStream(new File("C:/temp/test.txt")), writer, "UTF-8");
String fileContent = writer.toString();
System.out.println(fileContent);
// Method 2: IOUtils.toString()
String fileContent = IOUtils.toString(new FileInputStream(new File("C:/temp/test.txt")), "UTF-8");
System.out.println(fileContent);
5οΈβ£ Java Scanner
- The Underdog π€ΉββοΈβ
Not the most famous approach, but hey, it works! Scanner reads tokens from the stream, and in this case, we grab everything in one go. π©β¨
FileInputStream fin = new FileInputStream(new File("C:/temp/test.txt"));
java.util.Scanner scanner = new java.util.Scanner(fin, "UTF-8").useDelimiter("\\A");
String fileContent = scanner.hasNext() ? scanner.next() : "";
scanner.close();
π Wrap-Upβ
Thatβs it, folks! We've covered five awesome ways to convert an InputStream
to a String
in Java. Pick your favorite and start coding like a pro! ππ₯
Happy Coding! ππ±βπ»