Java InputStreamReader Create readTextFile(File path)

Here you can find the source of readTextFile(File path)

Description

Read a text file from a given path

License

Open Source License

Parameter

Parameter Description
path a parameter

Declaration

public static String readTextFile(File path) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**//w  ww.j  a  v a2  s  . c  o m
     * Read a text file from a given path
     * @param path
     * @return
     */
    public static String readTextFile(File path) {
        try {
            FileInputStream is = new FileInputStream(path);
            BufferedReader bin = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = bin.readLine()) != null) {
                sb.append(line);
                // re-add line separator
                sb.append(System.getProperty("line.separator"));
            }

            bin.close();

            return sb.toString();

        } catch (IOException e) {
            return null;
        }
    }
}

Related

  1. readTextFile(File file)
  2. readTextFile(File file)
  3. readTextFile(File file, String charsetName)
  4. readTextFile(File fromFile)
  5. readTextFile(File inputFile)
  6. readTextFile(final File f)
  7. readTextFile(final File file)
  8. ReadTextFile(InputStream is)
  9. readTextFile(java.io.File file)