Java FileInputStream Copy copyFileToString(java.io.File f)

Here you can find the source of copyFileToString(java.io.File f)

Description

Returns a string which contains the contents of a file.

License

Open Source License

Parameter

Parameter Description
f the file to be read

Return

the contents of the file(s).

Declaration

public final static String copyFileToString(java.io.File f) throws java.io.IOException 

Method Source Code

//package com.java2s;
/*// ww  w  . j  av a2  s  .  c  om
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */

import java.io.EOFException;

import java.io.FileInputStream;

public class Main {
    /**
     * Returns a string which contains the contents of a file.
     *
     * @param f the file to be read
     * @return the contents of the file(s).
     */
    public final static String copyFileToString(java.io.File f) throws java.io.IOException {
        int s = (int) f.length();
        byte[] data = new byte[s];
        int len = new FileInputStream(f).read(data);
        if (len != s)
            throw new EOFException("truncated file");
        return new String(data);
    }
}

Related

  1. copyFileToOutputStream(File file, OutputStream stream)
  2. copyFileToOutputStream(String fileLocation, OutputStream os)
  3. copyFileToStream(File currentFile, OutputStream outputStream)
  4. copyFileToStream(File file, OutputStream stream)
  5. copyFileToStream(File file, OutputStream stream)
  6. copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file, final int bytesInOfZip)
  7. copyFileUsingFileStreams(final File source, final File dest)
  8. copyFileUsingStream(File source, File dest)
  9. copyFileUsingStream(String source_path, String dest_path)