Java File to String readFileAsString(String filePath)

Here you can find the source of readFileAsString(String filePath)

Description

read File As String

License

Open Source License

Declaration

public static String readFileAsString(String filePath) throws java.io.IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *   Gisgraphy Project /*from   www. j a  va2  s  . c o m*/
 * 
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 * 
 *   This library 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
 *   Lesser General Public License for more details.
 * 
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 * 
 *  Copyright 2008  Gisgraphy project 
 *  David Masclet <davidmasclet@gisgraphy.com>
 *  
 *  
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.FileReader;

public class Main {
    public static String readFileAsString(String filePath) throws java.io.IOException {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

Related

  1. fileToStringList(File f)
  2. fileToStringList(String filePath)
  3. fileToStringOneLine(String path)
  4. readFileAsString(String filePath)
  5. readFileAsString(String filePath)
  6. readFileAsString(String filePath)
  7. readFileAsString(String filePath)
  8. readFileAsString(String filePath)
  9. readFileAsString(String filePathname)