List of utility methods to do Text File Read by Charset
BufferedReader | getReader(InputStream in, String charsetName) get Reader return getReader(in, Charset.forName(charsetName));
|
BufferedReader | getReader(InputStream is, String charset) get Reader return new BufferedReader(new InputStreamReader(is, Charset.forName(charset))); |
Reader | getReader(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; |
BufferedReader | getReader(String path, Charset encoding) Returns a reader that uses the given encoding. return new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)), encoding)); |
boolean | inferCharset(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) { ... |
CharsetDecoder | initDecoder(Charset charset, ThreadLocal init Decoder CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); localDecoder.set(new SoftReference<CharsetDecoder>(decoder)); return decoder; |
String | inputStreamToString(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); ... |
BufferedReader | newBufferedFileReader(File file, Charset charset) new Buffered File Reader return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); |
BufferedReader | newBufferedReader(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); |
BufferedReader | newReader(File file, Charset charset) new Reader checkNotNull(file); checkNotNull(charset); return new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); |