Java Utililty Methods Text File Read by Charset

List of utility methods to do Text File Read by Charset

Description

The list of methods to do Text File Read by Charset are organized into topic(s).

Method

BufferedReadergetReader(InputStream in, String charsetName)
get Reader
return getReader(in, Charset.forName(charsetName));
BufferedReadergetReader(InputStream is, String charset)
get Reader
return new BufferedReader(new InputStreamReader(is, Charset.forName(charset)));
ReadergetReader(String fileName, Charset cs)
get Reader
Reader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), cs));
} catch (FileNotFoundException e) {
    e.printStackTrace();
return reader;
BufferedReadergetReader(String path, Charset encoding)
Returns a reader that uses the given encoding.
return new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), encoding));
booleaninferCharset(byte[] bytes, int bytesRead, Charset clientCharset)
Try to determine whether a byte buffer's character encoding is that of the passed-in charset.
ByteBuffer byteBuf = ByteBuffer.wrap(bytes, 0, bytesRead);
CharBuffer charBuf = CharBuffer.allocate(byteBuf.capacity() * 2);
if (clientCharset != null) {
    CharsetDecoder decoder = clientCharset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    CoderResult coderResult = decoder.decode(byteBuf, charBuf, false);
    if (coderResult != null) {
...
CharsetDecoderinitDecoder(Charset charset, ThreadLocal> localDecoder)
init Decoder
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
localDecoder.set(new SoftReference<CharsetDecoder>(decoder));
return decoder;
StringinputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)
Reads the input stream till the end and return the reading as a string.
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
String line = null;
while ((line = reader.readLine()) != null) {
    if (sb.length() > 0) {
        sb.append("\n");
    sb.append(line);
...
BufferedReadernewBufferedFileReader(File file, Charset charset)
new Buffered File Reader
return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
BufferedReadernewBufferedReader(URL url, Charset cs)
Opens the given url for reading returning a BufferedReader that may be used to read text from the URL in an efficient manner.
CharsetDecoder decoder = cs.newDecoder();
Reader reader = new InputStreamReader(url.openStream(), decoder);
return new BufferedReader(reader);
BufferedReadernewReader(File file, Charset charset)
new Reader
checkNotNull(file);
checkNotNull(charset);
return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));