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

Hello
Code Factory!!!
  1. Convert InputStream to String using BufferedReader
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
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();
}
}
}
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();
}
}
}
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
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();
}
}
}
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();
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

From Prototype to Work of Art

How Much Does It Cost To Create An Online Radio Station App

Edutainment For Kids!

How did I debug a cors issue in tomcat container

Unity3D: Smart AI with the Nav Mesh System

Project Management tools for small teams — 2021

Debugging the ESP32 in PlatformIO (using JTAG)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Java — Escaping References

Signed and Unsigned Arithmetic Operators in JAVA

Java 14 Features