read String using BufferedReader - Java File Path IO

Java examples for File Path IO:BufferedReader

Description

read String using BufferedReader

Demo Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] argv) {
        String fileName = "java2s.com";
        System.out.println(readString(fileName));
    }/*from  w w w .  j  av  a  2 s  .com*/

    public static String readString(String fileName) {
        File file = new File(fileName);
        return readString(file);
    }

    public static String readString(File file) {
        StringBuilder builder = new StringBuilder();
        if (!file.exists()) {
            System.err.println("Can't Find " + file);
        }

        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                //System.out.println(str);
                builder.append(str);
            }
            in.close();
        } catch (IOException e) {
            e.getStackTrace();
        }
        return builder.toString();
    }
}

Related Tutorials