Java File Content Read getFileContents(final File file)

Here you can find the source of getFileContents(final File file)

Description

Read the contents of the specified file.

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description

Declaration

private static String getFileContents(final File file) throws java.io.IOException 

Method Source Code


//package com.java2s;
/*/*from w w w.ja v a  2s . co  m*/
 * Copyright 2010 jccastrejon
 *  
 * This file is part of MexADL.
 *
 * MexADL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MexADL 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 General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with MexADL.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;

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

public class Main {
    /**
     * Read the contents of the specified file.
     * 
     * @param file
     * @return
     * @throws java.io.IOException
     */
    private static String getFileContents(final File file) throws java.io.IOException {
        byte[] buffer;
        BufferedInputStream inputStream;

        inputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }

        return new String(buffer);
    }
}

Related

  1. getFileContents(File file)
  2. getFileContents(File file)
  3. getFileContents(File methodPatch)
  4. getFileContents(File testFile)
  5. getFileContents(final File f)
  6. getFileContents(String filename)
  7. getFileContents(String fileName)