Java Text File Read by Charset readFileAsString(String path, String charsetName)

Here you can find the source of readFileAsString(String path, String charsetName)

Description

This method reads the file as String and assumes that file contains information in specified encoding.

License

Open Source License

Parameter

Parameter Description
path String that contains the path to the file to be read
charsetName String that contains the encoding name

Exception

Parameter Description
IOException if any problem occurred

Return

String that contains the read content of the file

Declaration

public static String readFileAsString(String path, String charsetName) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.MessageFormat;

public class Main {
    private static final int MAX_READ_FAILURES = 10;

    /**/*from ww w .  ja  v a  2  s . co m*/
     * This method reads the file as String and assumes that file contains information in specified encoding. It can read files that are no larger
     * than 2147483647 bytes
     *
     * @param path        String that contains the path to the file to be read
     * @param charsetName String that contains the encoding name
     * @return String that contains the read content of the file
     * @throws IOException if any problem occurred
     */
    public static String readFileAsString(String path, String charsetName) throws IOException {
        ByteBuffer content = internalReadFileAsByteArray(path);
        CharBuffer result = Charset.forName(charsetName).decode(content);
        char[] charContent = null;
        if (result.hasArray()) {
            charContent = result.array();
        } else {
            charContent = new char[result.limit()];
            result.get(charContent);
        }
        return new String(charContent);

    }

    /**
     * This method reads the file as String. The default character set is used. It can read files that are no larger than 2147483647 bytes
     *
     * @param path String that contains the path to the file to be read
     * @return String that contains the read content of the file
     * @throws IOException if any problem occurred
     */
    public static String readFileAsString(String path) throws IOException {
        return readFileAsString(path, Charset.defaultCharset().name());
    }

    /**
     * This method that actually performs binary file content reading. It can read files that are no larger than 2147483647 bytes
     *
     * @param path String that contains the path to the file to be read
     * @return byte array that contains the file content
     * @throws IOException if any problem occurred
     */
    private static ByteBuffer internalReadFileAsByteArray(String path) throws IOException {
        ByteBuffer byteBuffer = null;
        Path filePath = FileSystems.getDefault().getPath(path);
        try (FileChannel fileChannel = FileChannel.open(filePath, StandardOpenOption.READ);) {
            Long size = fileChannel.size();
            if (size > Integer.MAX_VALUE) {
                throw new IOException(MessageFormat.format(
                        "File {0} is too large. Its size is {1,number,integer} bytes which is larger "
                                + "then this method could handle ( {2,number,integer})",
                        path, size, Integer.MAX_VALUE));
            }
            byteBuffer = ByteBuffer.allocate(size.intValue());
            int readBytes = 0;
            int totalReadBytes = 0;
            int failureCounter = 0;
            while ((readBytes = fileChannel.read(byteBuffer)) >= 0 && totalReadBytes < size.intValue()) {
                if (readBytes > 0) {
                    totalReadBytes += readBytes;
                    if (failureCounter > 0) {
                        failureCounter = 0;
                    }
                } else {
                    if (++failureCounter >= MAX_READ_FAILURES) {
                        throw new IOException(
                                MessageFormat.format("File {0} could not be read for unknown reason", path));
                    }
                }
            }
        }
        return (ByteBuffer) byteBuffer.flip();
    }
}

Related

  1. readFile(File file, String charsetName)
  2. readFile(File theFile, Charset theCharset)
  3. readFile(final InputStream is, final Charset encoding)
  4. readFile(String path, Charset charset)
  5. readFile(String path, Charset encoding)
  6. readFileContents(File file, Charset charset)
  7. readFileToLines(String inFile, String inCharset)
  8. readFileToList(String filePath, String charsetName)
  9. readFileToString(String filename, Charset encoding)