Java BufferedReader Read readFile(String path)

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

Description

Read a text file from the given path and return the contents as a String.

License

Open Source License

Parameter

Parameter Description
path The file path

Return

The (text) file contents

Declaration

public static String readFile(String path) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class Main {
    /**/* w  w  w . j av a 2  s. c  om*/
     * Read a text file and return the contents as a String.
     * 
     * @param file The file
     * @return The (text) file contents
     */
    public static String readFile(File file) {
        BufferedReader br = null;
        String out = "";

        try {
            String currentLine;
            br = new BufferedReader(new FileReader(file));
            while ((currentLine = br.readLine()) != null)
                out += currentLine + "\n";
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return out;
    }

    /**
     * Read a text file from the given path and return the contents as a String.
     * 
     * @param path The file path
     * @return The (text) file contents
     */
    public static String readFile(String path) {
        return readFile(new File(path));
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  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)