Java Text File Read by Charset readWholeFile(String filename, String charSet)

Here you can find the source of readWholeFile(String filename, String charSet)

Description

Reads whole file in and returns it as a string, even if file is binary.

License

Apache License

Parameter

Parameter Description
filename filename
charSet E.g. "UTF-8" or "ISO-8859-1"

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Return

file as whole string, with line endings in tack.

Declaration

public static String readWholeFile(String filename, String charSet) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/***  Java Commons and Niceties Library from CrunchyNoodles.com
 ***  Copyright (C) 2014 in USA by Brian Witt , bwitt@value.net
 ***/*  w ww.ja va 2 s  . c  om*/
 ***  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 languatge governing permissions and
 ***  limitations under the License.
 ***/

import java.io.*;
import java.nio.charset.Charset;

public class Main {
    /***
     *  Reads whole file in and returns it as a string, even if file is binary.
     *
     * @param filename filename
     * @param charSet E.g. "UTF-8" or "ISO-8859-1"
     * @return file as whole string, with line endings in tack.
     * @throws FileNotFoundException
     * @throws IOException
     * @see http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file
     */
    public static String readWholeFile(String filename, String charSet) throws FileNotFoundException, IOException {
        FileInputStream ins = new FileInputStream(filename);
        String contents = null;
        try {
            contents = readWholeFile(ins, Charset.forName(charSet));
        } finally {
            try {
                ins.close();
            } catch (Exception ex) {
            }
        }

        return contents;
    }

    /***
     *  Converts a {@link FileInputStream} into a big string.
     *  Handles various input-stream character sets, e.g. Latin-3 or UTF-8.
     *
     * @param ins stream to read
     * @param cs character set for single-byte to Unicode conversion.
     * @return A big string
     * @throws IOException
     * @see java.nio.charset.StandardCharsets
     */
    public static String readWholeFile(FileInputStream ins, Charset cs) throws IOException {
        Reader reader = new BufferedReader(new InputStreamReader(ins, cs));
        StringBuilder sb = new StringBuilder(9999);
        char[] buffer = new char[8100];
        int cnt;

        while ((cnt = reader.read(buffer, 0, buffer.length)) > 0) {
            sb.append(buffer, 0, cnt);
        }

        return sb.toString();
    }
}

Related

  1. readTextStream(InputStream is, Charset charset)
  2. readToBuffer(StringBuffer buffer, String filePath, Charset charset)
  3. readToString(InputStream in, Charset charset)
  4. readToString(ReadableByteChannel in, Charset charset)
  5. readURL(URL url, Charset encoding)