Java InputStreamReader Read readFile(String fileName, String characterEncoding)

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

Description

Reads a file and sends it to a buffer for parsing.

License

Open Source License

Parameter

Parameter Description
fileName file to parse
characterEncoding a parameter

Exception

Parameter Description
UnsupportedEncodingException an exception
FileNotFoundException an exception
IOException an exception

Return

a string buffer

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2011 eBay Inc./* w ww  .jav  a2s  .c  o m*/
 * 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
 *
 *******************************************************************************/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Reads a file and sends it to a buffer for parsing.
     * 
     * @param fileName file to parse
     * @param characterEncoding 
     * @return a string buffer
     * @throws UnsupportedEncodingException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static String readFile(String fileName, String characterEncoding)
            throws UnsupportedEncodingException, FileNotFoundException, IOException {
        FileInputStream fis;
        InputStreamReader isr;
        StringBuffer sb = new StringBuffer(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)
  3. readfile(String filename)
  4. readFile(String filename, boolean addEOL)
  5. readFile(String fileName, Class clazz)
  6. readFile(String fileName, String characterEncoding)
  7. readFile(String fileName, String characterEncoding)
  8. readFile(String fileName, String charSet)
  9. readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)