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

ReaderasReader(InputStream input, Charset charset)
Convenient method to read a byte stream as a character stream.
return new InputStreamReader(input, charset);
voidconvert(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);
...
voidcopy(Reader input, OutputStream output, Charset encoding)
copy
OutputStreamWriter out = new OutputStreamWriter(output, encoding);
copyLarge(input, out, new char[DEFAULT_BUFFER_SIZE]);
out.flush();
ReadercreateBOMStrippedReader(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";
...
BufferedReadercreateBufferedReaderWithGuessedCharset(File file)
Guess the file's character set and create BufferedReader
return createBufferedReaderWithGuessedCharset(file, "UTF-8");
InputStreamReadercreateInputStreamReader(File file, String charsetName)
create Input Stream Reader
Charset charset = charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
return createInputStreamReader(file, charset);
ReadercreateReader(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;
...
CharsetDecodergetDecoder(Charset charset, ThreadLocal> localDecoder)
get Decoder
Reference<CharsetDecoder> ref = localDecoder.get();
CharsetDecoder decoder;
return ref != null && (decoder = ref.get()) != null && decoder.charset() == charset ? decoder
        : initDecoder(charset, localDecoder);
StringgetFileContent(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) {
...
InputStreamReadergetInputStreamReader(InputStream stream, Charset charset)
get Input Stream Reader
return new InputStreamReader(stream, charset);