Java Text File Read by Charset getReader(String path, Charset encoding)

Here you can find the source of getReader(String path, Charset encoding)

Description

Returns a reader that uses the given encoding.

License

Open Source License

Parameter

Parameter Description
path the location of the file to be read
encoding the character set used for reading the file

Return

a reader that uses the given encoding

Declaration

public static BufferedReader getReader(String path, Charset encoding) throws FileNotFoundException 

Method Source Code


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

import java.io.BufferedReader;

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

import java.io.InputStreamReader;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    /**//from   w w w . j  av  a  2 s . c o m
     * Returns a reader that uses the UTF8 encoding.
     * 
     * @param path the location of the file to be read
     * @return a reader that uses the UTF8 encoding
     */
    public static BufferedReader getReader(String path) throws FileNotFoundException {

        return getReader(path, StandardCharsets.UTF_8);
    }

    /**
     * Returns a reader that uses the given encoding.
     * 
     * @param path the location of the file to be read
     * @param encoding the character set used for reading the file
     * @return a reader that uses the given encoding
     */
    public static BufferedReader getReader(String path, Charset encoding) throws FileNotFoundException {

        return new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), encoding));
    }
}

Related

  1. getFileContent(IFile file, String charset)
  2. getInputStreamReader(InputStream stream, Charset charset)
  3. getReader(InputStream in, String charsetName)
  4. getReader(InputStream is, String charset)
  5. getReader(String fileName, Charset cs)
  6. inferCharset(byte[] bytes, int bytesRead, Charset clientCharset)
  7. initDecoder(Charset charset, ThreadLocal> localDecoder)
  8. inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)
  9. newBufferedFileReader(File file, Charset charset)