Here you can find the source of readTextFile(Reader reader)
Parameter | Description |
---|---|
reader | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static StringBuffer readTextFile(Reader reader) throws IOException
//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); } } }