HOME FORUM BLOGS CONTACT LINKS FEEDBACK LOGIN REGISTER
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Read Data from InputStream to a String



Newly Added TutorialNewly Added Tutorial


Java Exception Handling
Factory Design Pattern
Thread Creation in Java
Read a File in Java
Convert List to Array
Read Excel in Java
Setting thread priorities
Method Invocation Using Reflection
Ajax Basics
Stateless Session Beans Callback
Ajax Autocomplete in Java
Hello World Java
Hello World Java
Decision Table
Object Serialization
Singleton Pattern
Java Heap Size
Set Heap Size Eclipse
OutofMemory Exception in Java
Axis Web Services
Java C++ Difference
SOA Tutorials
Filenet Platform BPM
Eclipse Tips & Tricks
enterprise service bus (ESB)
Enterprise Application Integration (EAI)
JPA Basics
ORM
Marshalling & Unmarshalling XML
Marshalling & Unmarshalling XML
JAXP- Read Write XML

JAVA Tutorials

Collection Framework
Operators in Java
If-else Statement

Thread
File
Generics
Input-Output in java
File Stream
Java Methods
Java Arrays
Java Objects
Java Exception Handling
Java Inheritance Tutorial
Java Threads
Garbage collection

J2EE Tutorials

Servlets Tutorials
Introduction to Struts Framework
JSP Tutorials
SPRING Framework Tutorials
STRUTS Tutorials
HIBERNATE Tutorials
WEB-SERVICES Tutorials

Java Collections

MAP
SET
LIST
VECTOR

Advanced Java

Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns

BRMS Tutorial

ILOG
BLAZE ADVISOR
PEGA
Drools
BRMS
BPM
JRules for .NET
ilog jrules interview questions
ilog jrules certification dumps
Java Certification Study Guide
hibernate tutorial
hello world struts example

All Tutorial

php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate

 
 
 

Useful Java Links




This code example shows how to read data from an InputStream object and ultimately store it in a String object.To read data from our file we use the method getResourceAsStream() which we get from our class object.The class object is retrieved by calling getClass() on our Main class.The method getResourceAsStream() enables us to read a file located within the same jar-file as the actual program, in case it is packaged as such.
It can also be used to read from the root directory of the application as done in the example by adding a slash before the filename.

Finally we use a BufferedReader to read from the stream line by line in a loop and append each line read to a StringBuilder object.
Since the method readLine() of the BufferedReader doesnt include the line breaks in the file we append a line break after appending the actual line data.

Finally the contents of the StringBuilder is printed out by converting the StringBuilder to a String using its toString() method.

Code Examples:Read Data from InputStream to a String

package com.javagenious.examples;

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

public class Main {

    public static void main(String[] args) {
        Main m = new Main();

        InputStream inStream = m.getClass().getResourceAsStream("/myTextFile.txt");

        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        StringBuilder builder = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(" "); //appende a new line
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(builder.toString());
    }
}



If you need any urgent assistance on Reading_A_File_Using_Inputstream_in_Java, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

Keyword Tags: Reading_A_File_Using_Inputstream_in_Java tutorial,Reading_A_File_Using_Inputstream_in_Java in java,Concepts of Reading_A_File_Using_Inputstream_in_Java,Java,J2EE,Interview Questions,Reading_A_File_Using_Inputstream_in_Java Examples


Java Virtual Machine (JVM)

Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. read more


Java Related Discussions

 
Javagenious.com

Interview Questions
Placement Papers
FAQs on J2EE
FAQs on JNDI
XML-Tutorials
Remote Web Services
Exception-Handling
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial

Oracle
MYSQL Tutorial
php Tutorial
JQuery Examples
JQuery Tuorial
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
Bea Weblogic
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
HOME | FORUM | ABOUT | BLOGS | CONTACT-US