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

ReadernewReader(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);
BufferedReadernewReader(ReadableByteChannel ch, Charset cs)
new Reader
if (ch == null)
    return null;
return new BufferedReader(Channels.newReader(ch, cs.newDecoder(), -1));
BufferedReadernewUTF8CharSetReader(String filename)
new UTF Char Set Reader
return new BufferedReader(new InputStreamReader(new FileInputStream(filename), Charset.forName("UTF-8")));
ReaderopenNewCompressedInputReader(final InputStream inputStream, final Charset charset)
open New Compressed Input Reader
final ZipInputStream zipInputStream = new ZipInputStream(inputStream);
zipInputStream.getNextEntry();
return new InputStreamReader(zipInputStream, charset);
Stringread(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);
...
Stringread(InputStream _is, CharsetDecoder _decoder)
read
StringBuilder sb = new StringBuilder();
read(new InputStreamReader(_is, _decoder), sb);
return sb.toString();
Stringread(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();
voidread(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();
Stringread(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();
...
StringreadAll(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();