Java InputStreamReader Create readTextFile(File file, String charsetName)

Here you can find the source of readTextFile(File file, String charsetName)

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(File file, String charsetName) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j  a va2 s .  c  om*/
 * FileUtil.java
 *
 * Copyright (c) 1998 - 2005 BusinessTechnology, Ltd.
 * All rights reserved
 *
 * This program is the proprietary and confidential information
 * of BusinessTechnology, Ltd. and may be used and disclosed only
 * as authorized in a license agreement authorizing and
 * controlling such use and disclosure
 *
 * Millennium Business Suite Anywhere System.
 *
 */

import java.io.BufferedReader;

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

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**
     * The default size of the copy buffer.
     */
    public static final int DEFAULT_BUFFER_SIZE = 8192;

    public static String readTextFile(String fullPathFilename) throws IOException {
        return readTextFile(new File(fullPathFilename));
    }

    public static String readTextFile(String fullPathFilename, String charsetName) throws IOException {
        return readTextFile(new File(fullPathFilename), charsetName);
    }

    public static String readTextFile(File file) throws IOException {
        return readTextFile(file, null);
    }

    public static String readTextFile(File file, String charsetName) throws IOException {
        StringBuffer sb = new StringBuffer(DEFAULT_BUFFER_SIZE);
        FileInputStream fis = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(
                charsetName == null ? new InputStreamReader(fis) : new InputStreamReader(fis, charsetName));

        char[] chars = new char[DEFAULT_BUFFER_SIZE];
        int numRead = 0;
        while ((numRead = reader.read(chars)) > -1) {
            sb.append(String.valueOf(chars, 0, numRead));
        }

        reader.close();

        return sb.toString();
    }
}

Related

  1. readTextFile(File f)
  2. readTextFile(File f)
  3. readTextFile(File f, int maxNumLines)
  4. readTextFile(File file)
  5. readTextFile(File file)
  6. readTextFile(File fromFile)
  7. readTextFile(File inputFile)
  8. readTextFile(File path)
  9. readTextFile(final File f)