List of utility methods to do Text File Read by Charset
Reader | newReader(InputStream input, Charset encoding) Creates a java.io.Reader from an input stream if (encoding != null) { return new InputStreamReader(input, encoding); } else { return new InputStreamReader(input); |
BufferedReader | newReader(ReadableByteChannel ch, Charset cs) new Reader if (ch == null) return null; return new BufferedReader(Channels.newReader(ch, cs.newDecoder(), -1)); |
BufferedReader | newUTF8CharSetReader(String filename) new UTF Char Set Reader return new BufferedReader(new InputStreamReader(new FileInputStream(filename), Charset.forName("UTF-8"))); |
Reader | openNewCompressedInputReader(final InputStream inputStream, final Charset charset) open New Compressed Input Reader final ZipInputStream zipInputStream = new ZipInputStream(inputStream); zipInputStream.getNextEntry(); return new InputStreamReader(zipInputStream, charset); |
String | read(File file, Charset charset) read FileLock lock = null; FileChannel fileChannel = null; try { fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE); lock = fileChannel.lock(); ByteBuffer buffer = ByteBuffer.allocate((int) fileChannel.size()); fileChannel.read(buffer); return new String(buffer.array(), charset); ... |
String | read(InputStream _is, CharsetDecoder _decoder) read StringBuilder sb = new StringBuilder(); read(new InputStreamReader(_is, _decoder), sb); return sb.toString(); |
String | read(InputStream in, Charset charset) Reads contents of the input stream fully and returns it as String. if (in == null) throw new IllegalArgumentException("input stream cannot be null"); InputStreamReader reader = new InputStreamReader(in, charset); char[] buffer = new char[1024]; StringBuilder sb = new StringBuilder(); for (int x = reader.read(buffer); x != -1; x = reader.read(buffer)) { sb.append(buffer, 0, x); return sb.toString(); |
void | read(InputStream in, Charset cs, Appendable appendable, int capacity) Read the specified text input stream and fill the content to the specified appendable. InputStreamReader isr = new InputStreamReader(in, cs == null ? Charset.defaultCharset() : cs); CharBuffer cbuf = CharBuffer.allocate(capacity); int r = 0; while (r > -1) { cbuf.clear(); appendable.append(cbuf, 0, r); r = isr.read(cbuf); isr.close(); |
String | read(InputStream in, String charsetName) read try (InputStreamReader inputReader = charsetName == null ? new InputStreamReader(in) : new InputStreamReader(in, charsetName)) { try (BufferedReader br = new BufferedReader(inputReader)) { StringBuffer sb = null; String s = br.readLine(); while (s != null) { if (sb == null) { sb = new StringBuffer(); ... |
String | readAll(InputStream in, Charset charset) read All StringWriter output = new StringWriter(); byte[] buffer = new byte[4096]; int n; while (-1 != (n = in.read(buffer))) { output.write(new String(buffer, 0, n, charset)); return output.toString(); |