How To Read Text File in Java?

Below code example shows how to read the text file in java. First the file is read into inputstream.

 * Created on 04-2011 Copyright(c) All Rights Reserved.
package com.kodehelp.javaio;


 * Created by
 * Date: 4/28/11
public class ReadTextFile {

    public static void main(String[] args) throws Exception{
        InputStream is = new FileInputStream("/");
        StringBuffer buffer = new StringBuffer();
        byte[] b = new byte[1024];
        int n;
         while ((n = != -1) {
            buffer.append(new String(b, 0, n));
        String str = buffer.toString();

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.