Java File to String readFileAsString(String path)

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

Description

Given the path to a file (e.g.

License

EUPL

Parameter

Parameter Description
path a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String readFileAsString(String path) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w w  w  . ja  v a  2  s . c o m*/
 * Digital Assets Management
 * =========================
 * 
 * Copyright 2009 Fundaci?n Iavante
 * 
 * Authors: 
 *   Francisco Jos? Moreno Llorca <packo@assamita.net>
 *   Francisco Jes?s Gonz?lez Mata <chuspb@gmail.com>
 *   Juan Antonio Guzm?n Hidalgo <juan@guzmanhidalgo.com>
 *   Daniel de la Cuesta Navarrete <cues7a@gmail.com>
 *   Manuel Jos? Cobo Fern?ndez <ranrrias@gmail.com>
 *
 * Licensed under the EUPL, Version 1.1 or ? as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and 
 * limitations under the Licence.
 * 
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**
     * Given the path to a file (e.g. XML file), it will return the contents as a
     * string.
     * 
     * @param path
     * @return
     * @throws IOException
     */
    public static String readFileAsString(String path) throws IOException {
        StringBuffer sb = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(path));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            sb.append(buf, 0, numRead);
        }
        reader.close();
        return sb.toString();
    }
}

Related

  1. readFileAsString(String filePath)
  2. readFileAsString(String filePathname)
  3. readFileAsString(String fname)
  4. readFileAsString(String fname)
  5. readFileAsString(String fpath)
  6. readFileAsString(String path)
  7. readFileAsString(String path)
  8. readFileAsString(String path)