Java InputStreamReader Create readTextFile(String fullPathFilename)

Here you can find the source of readTextFile(String fullPathFilename)

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(String fullPathFilename)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    /**// ww  w.  j  a  va 2s  .  c om
     * Fetch the entire contents of a text stream, and return it in a String.
     * This style of implementation does not throw Exceptions to the caller.
     */
    public static String readTextFile(InputStream is) {
        //...checks on aFile are elided
        StringBuilder contents = new StringBuilder();

        try {
            //use buffering, reading one line at a time
            //FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(is));
            try {
                String line = null; //not declared within while loop
                /*
                 * readLine is a bit quirky :
                 * it returns the content of a line MINUS the newline.
                 * it returns null only for the END of the stream.
                 * it returns an empty String if two newlines appear in a row.
                 */
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return contents.toString();
    }

    public static String readTextFile(String fullPathFilename)
            throws IOException {
        return readTextFile(new FileInputStream(fullPathFilename));
    }
}

Related

  1. readTextFile(java.io.File file)
  2. readTextFile(Reader reader)
  3. readTextFile(String fileName)
  4. readTextFile(String filename, int maxNumLines, boolean startAtBeginning)
  5. readTextFile(String filePath, String charset)
  6. readTextFile(String realPath)
  7. readTextFileAsStringArray(final File file, final String charSet)