How to Read/Convert an InputStream to a String?

Here is a some example to explain how to Read/Convert an InputStream to a String.

  • First example with BufferedReader and InputStreamReader:
package com.nourit.javaIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamToString {

  public static void main(String[] args) throws IOException {
    File originalFile = new File("D:/tmp/nourit.txt");

    InputStream inputStream = new FileInputStream(originalFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    System.out.println("------------------ Begin of Content ----------------------");
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    if (br != null) {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    System.out.println("------------------ End of Content ----------------------");
  }
}
  • Second example with using org.apache.commons.io.IOUtils
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class InputStreamToString1 {

  public static void main(String[] args) {
    File originalFile = new File("D:/tmp/nourit.txt");
    InputStream inputStream;
    try {
      inputStream = new FileInputStream(originalFile);
      String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
      System.out.println("------------------ Begin of Content ----------------------");
      System.out.println(result);
      System.out.println("------------------ End of Content ----------------------");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
  • Third example with IOUtils.copy and StringWriter
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;

public class InputStreamToString2 {
  public static void main(String[] args) {
    File originalFile = new File("D:/tmp/nourit.txt");
    InputStream inputStream;
    try {
      inputStream = new FileInputStream(originalFile);
      
      StringWriter writer = new StringWriter();
      IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8);
      System.out.println("------------------ Begin of Content ----------------------");
      System.out.println(writer.toString());
      System.out.println("------------------ End of Content ----------------------");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Fourth examlpe with using  java.util.Scanner
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class InputStreamToString3 {

  private static Scanner scanner;

  public static void main(String[] args) {
    File originalFile = new File("D:/tmp/nourit.txt");
    InputStream inputStream;
    try {
      inputStream = new FileInputStream(originalFile);
      scanner = new Scanner(inputStream);
      scanner = scanner.useDelimiter("\\A");
      String result = scanner.hasNext() ? scanner.next() : "";
      System.out.println("------------------ Begin of Content ----------------------");
      System.out.println(result);
      System.out.println("------------------ End of Content ----------------------");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Fifth example with using ByteArrayOutputStream and inputStream.read
package com.nourit.javaIO;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamToString4 {

  public static void main(String[] args) {
    File originalFile = new File("D:/tmp/nourit.txt");
    InputStream inputStream;
    try {
      inputStream = new FileInputStream(originalFile);
      ByteArrayOutputStream result = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int length;
      while ((length = inputStream.read(buffer)) != -1) {
          result.write(buffer, 0, length);
      }
      
      System.out.println("------------------ Begin of Content ----------------------");
      System.out.println(result.toString("UTF-8"));
      System.out.println("------------------ End of Content ----------------------");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Sixth example with using BufferedInputStream and ByteArrayOutputStream.
package com.nourit.javaIO;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamToString5 {
  private static BufferedInputStream bis;

  public static void main(String[] args) {
    File originalFile = new File("D:/tmp/nourit.txt");
    InputStream inputStream;
    try {
      inputStream = new FileInputStream(originalFile);
      bis = new BufferedInputStream(inputStream);
      ByteArrayOutputStream buf = new ByteArrayOutputStream();
      int result = bis.read();
      while(result != -1) {
          buf.write((byte) result);
          result = bis.read();
      }
      
      System.out.println("------------------ Begin of Content ----------------------");
      System.out.println(buf.toString("UTF-8"));
      System.out.println("------------------ End of Content ----------------------");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

------------------ Begin of Content ----------------------
IT Training Center for Java and Web technology. 
If you enjoy and like our tutorials and believe in our mission, 
Please Subscribe us on Youtube and follow us on Twitter, 
or befriend us on Facebook or Google Plus.
------------------ End of Content ----------------------

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*