Java InputStreamReader Create readTextFile(Reader reader)

Here you can find the source of readTextFile(Reader reader)

Description

reads a Text file from its resource.

License

Apache License

Parameter

Parameter Description
reader a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static StringBuffer readTextFile(Reader reader) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.EOFException;
import java.io.File;

import java.io.FileInputStream;

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

import java.io.Reader;

public class Main {

    public static String readTextFile(File f) throws IOException {

        StringBuffer buf = new StringBuffer();

        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f)/* , "UTF-8" */));
        String inputLine;//  w ww .  jav a  2 s. com
        while ((inputLine = in.readLine()) != null) {
            buf.append(inputLine);
            buf.append('\n');
        }
        in.close();
        return buf.toString();

    }

    /**
     * reads a Text file from its resource.
     * 
     * @param reader
     * @return
     * @throws IOException
     */
    public static StringBuffer readTextFile(Reader reader) throws IOException {
        StringBuffer sb = new StringBuffer(10000);
        int c = 0;
        while ((c = reader.read()) > -1) {
            sb.append((char) c);
        }
        reader.close();

        return sb;
    }

    /**
     *** Reads a single line of characters from the specified InputStream,
     * terminated by either a newline (\n) or carriage-return (\r)
     *** 
     * @param input
     *            The InputStream
     *** @return The line read from the InputStream
     **/
    public static String readLine(InputStream input) throws IOException {
        StringBuffer sb = new StringBuffer();
        while (true) {
            int ch = input.read();
            if (ch < 0) { // eof
                throw new EOFException("End of InputStream");
            } else if ((ch == '\r') || (ch == '\n')) {
                return sb.toString();
            }
            sb.append((char) ch);
        }
    }
}

Related

  1. readTextFile(File path)
  2. readTextFile(final File f)
  3. readTextFile(final File file)
  4. ReadTextFile(InputStream is)
  5. readTextFile(java.io.File file)
  6. readTextFile(String fileName)
  7. readTextFile(String filename, int maxNumLines, boolean startAtBeginning)
  8. readTextFile(String filePath, String charset)
  9. readTextFile(String fullPathFilename)