Java FileInputStream Read readFile(File file, String encoding)

Here you can find the source of readFile(File file, String encoding)

Description

read File

License

Apache License

Declaration

public static StringBuilder readFile(File file, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*/* ww w.  ja  v a 2 s  .  co m*/
 *
 *  * Copyright 1999-2011 jeap Group Holding Ltd.
 *  *
 *  * 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.
 *
 */

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

import java.io.IOException;

public class Main {

    public static StringBuilder readFile(File file, String encoding) throws IOException {
        if (encoding == null)
            encoding = "UTF-8";
        StringBuilder sb = new StringBuilder();
        if (!file.exists() || !file.isFile())
            return sb;
        final int buffer = 1024;
        byte[] bs = new byte[buffer];
        FileInputStream in = new FileInputStream(file);
        int len = in.read(bs);
        int total = 0;
        while (len != -1) {
            total += len;
            byte[] tmp = new byte[total + buffer];
            System.arraycopy(bs, 0, tmp, 0, total);
            bs = tmp;
            len = in.read(bs, total, buffer);
        }
        sb.append(new String(bs, 0, total, encoding));
        in.close();
        return sb;
    }
}

Related

  1. readFile(File file, boolean compress)
  2. readFile(File file, int size)
  3. readFile(File file, OutputStream output)
  4. readFile(File file, String encoding)
  5. readFile(File file, String encoding)
  6. readFile(File fyl)
  7. readFile(File path)
  8. readfile(File path)
  9. readFile(File source)