Java File Read by Charset loadFile(File logfile, Charset charset)

Here you can find the source of loadFile(File logfile, Charset charset)

Description

load File

License

Open Source License

Declaration

public static String loadFile(File logfile, Charset charset) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 * Copyright (c) 2004-2011 Oracle Corporation.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: //from  w  w  w .j ava2s. c o  m
 *
 *    Kohsuke Kawaguchi, Winston Prakash
 *     
 *
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.FileInputStream;

import java.nio.charset.Charset;

public class Main {
    /**
     * Loads the contents of a file into a string.
     */
    public static String loadFile(File logfile) throws IOException {
        return loadFile(logfile, Charset.defaultCharset());
    }

    public static String loadFile(File logfile, Charset charset) throws IOException {
        if (!logfile.exists()) {
            return "";
        }

        StringBuilder str = new StringBuilder((int) logfile.length());

        BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(logfile), charset));
        char[] buf = new char[1024];
        int len;
        while ((len = r.read(buf, 0, buf.length)) > 0) {
            str.append(buf, 0, len);
        }
        r.close();

        return str.toString();
    }
}

Related

  1. getUncommentedLines(File file, Charset forName)
  2. getZipFile(File src, Charset charset)
  3. isEqual(final File document, final Charset a, final Charset b)
  4. load(File file, Charset charset)
  5. loadFile(File file, Charset cs)
  6. loadString(File f, Charset charset)
  7. open(File file, Charset charset)
  8. parseNameDelimiterValueNewLineFile( File nameDelimiterValueNewLineFile, String nameValueDelimiter, Charset charset)
  9. saveFile(File file, String content, String charsetName)