Java File to String fileToString(String filename)

Here you can find the source of fileToString(String filename)

Description

Read the contents of a file into a single Java String.

License

Open Source License

Parameter

Parameter Description
filename The file to be read in

Exception

Parameter Description
FileNotFoundException and IOException on a read error. These are propegatedrather than simply trapped here bcause if reading in filesis somewhat critical to the app and if something goeswrong, the app needs to take remedial action, probably withuser feedback.

Return

a String containing the contents of filename

Declaration


public static String fileToString(String filename) throws IOException, FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*w w  w .j  av a 2 s.c  om*/
 * XML Sequences for mission critical IT procedures
 *
 * Copyright ? 2004-2010 Operational Dynamics Consulting, Pty Ltd
 *
 * The code in this file, and the program it is a part of, is made available
 * to you by its authors as open source software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License version
 * 2 ("GPL") as published by the Free Software Foundation.
 *
 * 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 GPL for more details.
 *
 * You should have received a copy of the GPL along with this program. If not,
 * see http://www.gnu.org/licenses/. The authors of this program may be
 * contacted through http://research.operationaldynamics.com/projects/xseq/.
 */

import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**
     * Read the contents of a file into a single Java String.
     * 
     * @param filename
     *            The file to be read in
     * @return a String containing the contents of filename
     * @throws FileNotFoundException
     *             and IOException on a read error. These are propegated
     *             rather than simply trapped here bcause if reading in files
     *             is somewhat critical to the app and if something goes
     *             wrong, the app needs to take remedial action, probably with
     *             user feedback.
     */

    public static String fileToString(String filename) throws IOException, FileNotFoundException {

        BufferedReader in = new BufferedReader(new FileReader(filename));

        StringBuffer sb = new StringBuffer();
        String str = null;
        while ((str = in.readLine()) != null) {
            // oops, that looses newlines, so put 'em back in
            sb.append(str);
            sb.append('\n');
        }

        in.close();

        return sb.toString();
    }
}

Related

  1. fileToString(String filename)
  2. fileToString(String fileName)
  3. fileToString(String fileName)
  4. fileToString(String fileName)
  5. fileToString(String filename)
  6. fileToString(String fileName)
  7. fileToString(String filename)
  8. fileToString(String fileName, String timestamp, String adapterName)
  9. fileToString(String filePath)