Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to convert InputStream data to String data in Java ?.


Program to how to convert InputStream data to String data in Java.


package com.hubberspot.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class InputStreamToString {

   public static void main(String[] args) throws IOException {

 InputStreamToString ists = new InputStreamToString();

 InputStream inputStream =
   ists.getClass().getResourceAsStream("/customer.txt");

 if (inputStream != null) {

  char[] charBuffer = new char[1024];
  Writer outputToConsole = new StringWriter();
  try {
   Reader readFromFile = new BufferedReader(
     new InputStreamReader(inputStream));
   int i;
   while ((i = readFromFile.read(charBuffer)) != -1) {
    outputToConsole.write(charBuffer, 0, i);
   }
  } finally{
   System.out.println(outputToConsole.toString());
   inputStream.close();
  }
 }     
   } 
}



customer.txt














Output of the program : 


 

 
© 2021 Learn Java by Examples Template by Hubberspot