Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 1 October 2013

Convert InputStream to String using Java IO


In this tutorials we are going to see how to convert InputStream to String using methods available in Java IO.
  1. Create project called JavaMisc.
  2. Create package called com.javatutorialscorner.io
  3. Create java class called InputStreamToString under com.javatutorialscorner.io package.
InputStreamToString.java
package com.javatutorialscorner.io;

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;

public class InputStreamToString {

/**
* @param args
*/
public static void main(String[] args) {
InputStream is = null;

try {
is = new FileInputStream(new File(
"C:\\jtc\\javatutorialscorner.txt"));
String contentInFile = toString(is);
System.out.println(contentInFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static String toString(InputStream is) {
BufferedReader br = null;
StringBuilder sb = null;
String templine = "";

try {
sb = new StringBuilder();

br = new BufferedReader(new InputStreamReader(is));
while ((templine = br.readLine()) != null) {
sb.append(templine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();
}
}


Now run the program see the following output in console.

this is test file created by java tutorials corner

This content read from input file

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Lambda expressions are allowed only at source level 1.8 or above The "source compatibility" part of the Java Compiler dialog has to be 1.8. Otherwise even though you're allowed to use the library features of Java 1.8, you won't be able to use the language features. It's not just lambdas -… Read More
  • Hibernate Architecture Overview The diagram below provides a high-level view of the Hibernate architecture: Minimal architecture The "minimal" architecture has the application manage its own JDBC connections and provide those connections to Hibernate;… Read More
  • Object-Oriented Programming Concepts Object-Oriented Programming (OOPs) Concepts Object Class Inheritance Polymorphism Abstraction Encapsulation Object Any entity that has state and behavior is known as an object. For example: table, keyboard, car etc. It … Read More
  • Hibernate Introduction Introduction Hibernate is a high-performance Object/Relational persistence and query service.The term Object/Relational mapping refers to the technique of mapping data and object model representation to relational data mod… Read More
  • Java Overview Talk about Java technology seems to be everywhere, but what exactly is it? The following sections explain how Java technology is both a programming language and a platform, and provide an overview of what this technology can… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Convert InputStream to String using Java IO Rating: 5 Reviewed By: eHowToNow