Java FileInputStream Read loadContents(File file)

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

Description

Loads a file contents as a String using platform encoding.

License

Open Source License

Parameter

Parameter Description
file file to load.

Exception

Parameter Description
Exception an exception

Return

the file contents as a .

Declaration

public static String loadContents(File file) throws IOException 

Method Source Code


//package com.java2s;
/* *********************************************************************
 *  This file is part of Hannah./*from www.  jav  a 2  s.  c  o  m*/
 *
 *  Hannah 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.
 *
 *  Hannah 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 Hannah.  If not, see <http://www.gnu.org/licenses/>.
 * ******************************************************************* */

import java.io.BufferedInputStream;

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

import java.io.IOException;

public class Main {
    /**
     * <p>Loads a file contents as a String using platform encoding.</p>
     * @param file file to load.
     * @return the file contents as a {@link String}.
     * @throws Exception 
     */
    public static String loadContents(File file) throws IOException {
        final StringBuilder contents = new StringBuilder();
        final BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        try {
            final byte[] buffer = new byte[1024];
            int read = in.read(buffer);
            while (read >= 0) {
                contents.append(new String(buffer, 0, read));
                read = in.read(buffer);
            }
        } finally {
            in.close();
        }
        return contents.toString();
    }
}

Related

  1. load(File file)
  2. load(File file)
  3. load(File file)
  4. load(String file)
  5. loadData(File f)
  6. loadFolderFromJar(String path)
  7. loadRes(String res)
  8. loadStackableItems(String fileName)