Java BufferedReader Read readFile(String path)

Here you can find the source of readFile(String path)

Description

Reads and returns the text contained in a given file.

License

Apache License

Parameter

Parameter Description
path The path to a text file.

Return

The text contained in the file.

Declaration

public static String readFile(String path) 

Method Source Code

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**//from ww w  .ja  v a2s  .  c  o  m
     * Reads and returns the text contained in a given file.
     * 
     * @param path
     *            The path to a text file.
     * @return The text contained in the file.
     */
    public static String readFile(String path) {
        StringBuilder res = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = in.readLine()) != null) {
                res.append(new String(line.getBytes(), "UTF-8") + "\n");
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res.toString();
    }
}

Related

  1. readFile(String fullPath)
  2. readFile(String nameFile)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String path)
  7. readFile(String path)
  8. readFile(String path)
  9. readFile(String path)