Different Ways to Convert InputStream to String in Java | Code Factory

Code Factory
3 min readApr 16, 2020

--

Reference Link : Link

Donate : Link

CodeFactory.txt

Hello
Code Factory!!!
  1. Convert InputStream to String using BufferedReader

BufferedReaderExample.java

package com.codeFactory;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author code.factory
*
*/
public class BufferedReaderExample {
public static void main(String... args) {
InputStream inputStream = null;
BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();
String text;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while ((text = bufferedReader.readLine()) != null) {
stringBuilder.append(text);
stringBuilder.append("\n");
}
System.out.println(stringBuilder);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. Convert InputStream to String using Scanner

ScannerExample.java

package com.codeFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
/**
* @author code.factory
*
*/
public class ScannerExample {
public static void main(String... args) {
InputStream inputStream = null;
Scanner scanner = null;
StringBuilder stringBuilder = new StringBuilder();
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\n");
while (scanner.hasNext()) {
stringBuilder.append(scanner.next());
}
System.out.println(stringBuilder);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

3. Convert InputStream to String using Stream API Collect

CollectAPIExample.java

package com.codeFactory;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
/**
* @author code.factory
*
*/
public class CollectAPIExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().collect(Collectors.joining("\n"));
System.out.println(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

4. Convert InputStream to String using Stream API Parallel

ParallelExample.java

package com.codeFactory;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
/**
* @author code.factory
*
*/
public class ParallelExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
String result = new BufferedReader(new InputStreamReader(inputStream))
.lines().parallel().collect(Collectors.joining("\n"));
System.out.println(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

5. Convert InputStream to String using ByteArrayOutputStream

ByteArrayOutputStreamExample.java

package com.codeFactory;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* @author code.factory
*
*/
public class ByteArrayOutputStreamExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
while ((inputStream.read(buffer)) != -1) {
result.write(buffer);
}
System.out.println(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

6. Convert InputStream to String using IOUtils toString

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

IOUtilsStringExample.java

package com.codeFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;/**
* @author code.factory
*
*/
public class IOUtilsStringExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);System.out.println(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

7. Convert InputStream to String using IOUtils Copy

IOUtilsCopyExample.java

package com.codeFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;/**
* @author code.factory
*
*/
public class IOUtilsCopyExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8);
System.out.println(writer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

8. Convert InputStream to String using Guava CharStreams

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>

GuavaCharStreamsExample.java

package com.codeFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import com.google.common.io.CharStreams;/**
* @author code.factory
*
*/
public class GuavaCharStreamsExample {
public static void main(String... args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
String result = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8));System.out.println(result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

9. Convert InputStream to String using Guava ByteSource

GuavaByteSourceExample.java

package com.codeFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.google.common.io.ByteSource;/**
* @author code.factory
*
*/
public class GuavaByteSourceExample {
public static void main(String... args) {
try {
InputStream inputStream = new FileInputStream(new File("files/CodeFactory.txt"));
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return inputStream;
}
};
String result = byteSource.asCharSource(StandardCharsets.UTF_8).read();System.out.println(result);} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

--

--