read From File via FileReader - Java java.io

Java examples for java.io:FileReader

Description

read From File via FileReader

Demo Code


//package com.java2s;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    public static void main(String[] argv) {
        String cert = "java2s.com";
        System.out.println(readFromFile(cert));
    }/*from   w  w w. java2 s  .  co m*/

    public static String readFromFile(final String cert) {
        FileReader fr = null;
        final StringBuilder sb = new StringBuilder();
        try {
            fr = new FileReader(cert);
            for (;;) {
                final int c = fr.read();
                if (c == -1) {
                    break;
                }
                sb.append((char) c);
            }
            return sb.toString();
        } catch (final IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (final IOException e) {
                }
            }
        }
    }
}

Related Tutorials