Java InputStreamReader Read readFile(String path)

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

Description

Returns the content of the given file.

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static String readFile(String path) throws IOException 

Method Source Code


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

import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    /**/*from  ww w  .  j a v  a2  s .  c o m*/
     * Returns the content of the given file.
     * @throws IOException
     */
    public static String readFile(String path) throws IOException {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

        if (is == null) {
            throw new FileNotFoundException("Can't find \"" + path + "\".");
        }

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        try {
            String line;
            br = new BufferedReader(new InputStreamReader(is));

            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }

        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return sb.toString();
    }
}

Related

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