Java InputStreamReader Read readFile(String fileName, String characterEncoding)

Here you can find the source of readFile(String fileName, String characterEncoding)

Description

read File

License

Apache License

Declaration

public static String readFile(String fileName, String characterEncoding)
            throws UnsupportedEncodingException, FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 eBay Software Foundation.
 *
 * 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./*w ww.j a  v  a  2  s.  co m*/
 *******************************************************************************/

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

public class Main {
    public static String readFile(String fileName, String characterEncoding)
            throws UnsupportedEncodingException, FileNotFoundException, IOException {
        FileInputStream fis;
        InputStreamReader isr;
        StringBuilder sb = new StringBuilder(75000);
        char[] buf = new char[4096];
        int numRead;
        fis = new FileInputStream(fileName);
        isr = new InputStreamReader(fis, characterEncoding);
        do {
            numRead = isr.read(buf, 0, buf.length);
            if (numRead > 0) {
                sb.append(buf, 0, numRead);
            }
        } while (numRead >= 0);
        isr.close();
        fis.close();
        return sb.toString();
    }
}

Related

  1. readfile(String filename)
  2. readFile(String filename, boolean addEOL)
  3. readFile(String fileName, Class clazz)
  4. readFile(String fileName, String characterEncoding)
  5. readFile(String fileName, String characterEncoding)
  6. readFile(String fileName, String charSet)
  7. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)
  8. readFile(String filename1)
  9. readFile(String fileNm)