Java FileInputStream Read readFile(String fname)

Here you can find the source of readFile(String fname)

Description

read File

License

Apache License

Parameter

Parameter Description
fname a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String readFile(String fname) throws IOException 

Method Source Code

//package com.java2s;
/**//ww w .  j a  va2s . co m
 *
 *  Copyright 2009-2013 The MITRE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * **************************************************************************
 *                          NOTICE
 * This software was produced for the U. S. Government under Contract No.
 * W15P7T-12-C-F600, and is subject to the Rights in Noncommercial Computer
 * Software and Noncommercial Computer Software Documentation Clause
 * 252.227-7014 (JUN 1995)
 *
 * (c) 2012 The MITRE Corporation. All Rights Reserved.
 * **************************************************************************
 */

import java.io.*;

public class Main {
    /**
     *
     */
    public static String default_encoding = "UTF-8";

    /**
     *
     * @param fname
     * @return
     * @throws IOException
     */
    public static String readFile(String fname) throws IOException {
        return readFile(new File(fname), default_encoding);
    }

    /**
     *
     * @param f - File object
     * @return
     * @throws IOException
     */
    public static String readFile(File f) throws IOException {
        return readFile(f, default_encoding);
    }

    /**
     * Slurps a text file into a string and returns the string.
     * @param fileinput 
     * @param enc
     * @return
     * @throws IOException  
     */
    public static String readFile(File fileinput, String enc) throws IOException {
        if (fileinput == null) {
            return null;
        }

        FileInputStream instream = new FileInputStream(fileinput);
        byte[] inputBytes = new byte[instream.available()];
        instream.read(inputBytes);
        instream.close();
        return new String(inputBytes, enc);
    }
}

Related

  1. readFile(String filePath)
  2. readFile(String filePath)
  3. readFile(String filePath)
  4. readFile(String filePathName)
  5. readFile(String fname)
  6. readFile(String fname)
  7. readFile(String imageName)
  8. readFile(String name)
  9. readFile(String path)