Pin It

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 : 


 

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot