Java BufferedReader Read readFile(String path)

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

Description

Reads a file and returns the content as a big String.

License

Open Source License

Parameter

Parameter Description
path a parameter

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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**/*  ww w .j av  a2  s.  c  o  m*/
     * Reads a file and returns the content as a big String.
     * @param path
     * @return 
     */
    public static String readFile(String path) {
        StringBuilder buffer = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));

            String line;
            while ((line = reader.readLine()) != null)
                buffer.append(line).append('\n');

        } catch (FileNotFoundException ex) {
            System.err.println("File " + path + " was not found...");
            System.exit(1);
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            System.exit(1);
        }

        return buffer.toString();
    }
}

Related

  1. readFile(String path)
  2. readFile(String path)
  3. readFile(String path)
  4. readFile(String path)
  5. readFile(String path)
  6. readFile(String pathname)
  7. readFile(String sFileName_)
  8. readFile(String uri)
  9. readFile(String xmlFile)