Java InputStreamReader Read readFile(String path)

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

Description

Copied from http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file

License

Open Source License

Declaration

public static String readFile(String path) 

Method Source Code

//package com.java2s;
/* license-start// w w  w  . j  a  v a 2 s.  c o  m
 * 
 * Copyright (C) 2008 - 2013 Crispico Software, <http://www.crispico.com/>.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details, at <http://www.gnu.org/licenses/>.
 * 
 * license-end
 */

import java.io.FileInputStream;

import java.io.InputStreamReader;

public class Main {
    /**
     * Copied from http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
     */
    public static String readFile(String path) {
        StringBuffer loadedContent = new StringBuffer();
        try {
            InputStreamReader fileEditorInputReader = new InputStreamReader(new FileInputStream(path));
            char[] buffer = new char[1024];
            int bytesRead;
            do {
                bytesRead = fileEditorInputReader.read(buffer);
                if (bytesRead > 0)
                    loadedContent.append(buffer, 0, bytesRead);
            } while (bytesRead > 0);
            fileEditorInputReader.close();
        } catch (Exception e) {
            throw new RuntimeException("Error while loading file content " + path, e);
        }
        return loadedContent.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 path)
  7. readFile(String path, String charset)
  8. readFile(ZipInputStream zin)
  9. readFileAddLine(String filePath, Object object)