Java File to String fileToString(File file)

Here you can find the source of fileToString(File file)

Description

Read in the contents of a file and return it as a String.

License

Open Source License

Parameter

Parameter Description
file The <code>File</code> to read in as a String

Exception

Parameter Description
IOException If an input or output exception occurred

Return

the resulting string

Declaration

public static String fileToString(File file) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww w  .  jav  a 2s  .c  om
 *  This file is part the Cytobank ACS Library.
 *  Copyright (C) 2010 Cytobank, Inc.  All rights reserved.
 *
 *  The Cytobank ACS Library program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /** The buffer size in bytes to create when copying streams. */
    public static final int BYTE_BUFFER_SIZE = 1024000;

    /**
     * Read in the contents of a file and return it as a String.
     * 
     * @param file The <code>File</code> to read in as a String
     * @return the resulting string
     * @throws IOException If an input or output exception occurred
     */
    public static String fileToString(File file) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        FileInputStream fileInputStream = new FileInputStream(file);

        try {
            writeInputStreamToOutputStream(fileInputStream, byteArrayOutputStream);
        } finally {
            try {
                fileInputStream.close();
            } catch (Throwable ignore) {
            }
        }

        return byteArrayOutputStream.toString();
    }

    /**
     * Writes an <code>InputStream</code> to an <code>OutputStream</code>.
     * 
     * @param inputStream the <code>InputStream</code> to read
     * @param outputStream the <code>OutputStream</code> to write to
     * @return the number of bytes written
     * @throws IOException If an input or output exception occurred
     */
    public static long writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream)
            throws IOException {
        final byte[] byteBuffer = new byte[BYTE_BUFFER_SIZE];

        int read;
        long written = 0;

        while ((read = inputStream.read(byteBuffer, 0, BYTE_BUFFER_SIZE)) > 0) {
            outputStream.write(byteBuffer, 0, read);
            written += read;
        }

        return written;
    }
}

Related

  1. fileToString(File f)
  2. fileToString(File f)
  3. fileToString(File f)
  4. fileToString(File f)
  5. fileToString(File f, String encoding)
  6. fileToString(File file)
  7. fileToString(File file)
  8. fileToString(File file)
  9. fileToString(File file)