List of utility methods to do Text File Read by Charset
Reader | asReader(InputStream input, Charset charset) Convenient method to read a byte stream as a character stream. return new InputStreamReader(input, charset); |
void | convert(File file, Charset from, String toEncoding, ByteArrayOutputStream bytearray, boolean headersOn, int totalLinesToRead) convert BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), from)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(bytearray, toEncoding)); for (int c = 0; c < totalLinesToRead; c++) { String tmpline = reader.readLine(); if (tmpline == null) break; writer.write(tmpline, 0, tmpline.length()); writer.write("\n", 0, 1); ... |
void | copy(Reader input, OutputStream output, Charset encoding) copy OutputStreamWriter out = new OutputStreamWriter(output, encoding); copyLarge(input, out, new char[DEFAULT_BUFFER_SIZE]); out.flush(); |
Reader | createBOMStrippedReader(InputStream stream, String defaultCharset) Create BOM stripped reader from the stream. InputStream in = stream.markSupported() ? stream : new BufferedInputStream(stream); String charset = null; in.mark(3); byte[] head = new byte[3]; int br = in.read(head, 0, 3); if (br >= 2 && (head[0] == (byte) 0xFE && head[1] == (byte) 0xFF) || (head[0] == (byte) 0xFF && head[1] == (byte) 0xFE)) { charset = "UTF-16"; ... |
BufferedReader | createBufferedReaderWithGuessedCharset(File file) Guess the file's character set and create BufferedReader return createBufferedReaderWithGuessedCharset(file, "UTF-8"); |
InputStreamReader | createInputStreamReader(File file, String charsetName) create Input Stream Reader Charset charset = charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
return createInputStreamReader(file, charset);
|
Reader | createReader(Path p, Charset cs) create Reader InputStream is = Files.newInputStream(p); InputStream gis; if (p.toString().endsWith(".gz")) gis = new GZIPInputStream(is); else gis = is; Reader r = new InputStreamReader(gis, cs); return r; ... |
CharsetDecoder | getDecoder(Charset charset, ThreadLocal get Decoder Reference<CharsetDecoder> ref = localDecoder.get();
CharsetDecoder decoder;
return ref != null && (decoder = ref.get()) != null && decoder.charset() == charset ? decoder
: initDecoder(charset, localDecoder);
|
String | getFileContent(IFile file, String charset) Returns the file content as UTF8 string. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream in = null; try { in = file.getContents(true); byte[] buf = new byte[8000]; for (int count; (count = in.read(buf)) != -1;) outputStream.write(buf, 0, count); } catch (Exception e) { ... |
InputStreamReader | getInputStreamReader(InputStream stream, Charset charset) get Input Stream Reader return new InputStreamReader(stream, charset); |