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

Readerreader(Class baseClass, String resourceName, Charset charset)
reader
return new InputStreamReader(inputStream(baseClass, resourceName), charset);
InputStreamReaderreader(InputStream in, String charsetName)
reader
InputStreamReader result;
if (charsetName == null) {
    result = reader(in);
} else {
    result = new InputStreamReader(in, Charset.forName(charsetName));
return result;
BufferedReaderreaderBuffered(final File file, final Charset charset)
Open a buffered Reader , equivalent to:
new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
return reader;
StringreadFile(Bundle bundle, String path, Charset cs)
read File
URL jsonUrl = bundle.getEntry(path);
try (Scanner scanner = new Scanner(jsonUrl.openStream(), cs.name())) {
    scanner.useDelimiter("\\A");
    if (scanner.hasNext()) {
        return scanner.next();
    } else {
        throw new IOException("no content");
StringreadFile(File f, Charset chst)
read File
FileInputStream fis = new FileInputStream(f);
return readStream(fis, chst, f.length());
StringreadFile(File file, Charset charset)
read File
FileInputStream stream = new FileInputStream(file);
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return charset.decode(bb).toString();
} finally {
    stream.close();
StringreadFile(File file, Charset charset)
read File
InputStream is = new FileInputStream(file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
try {
    while ((read = is.read(buffer)) != -1) {
        os.write(buffer, 0, read);
        os.flush();
...
StringreadFile(File file, Charset cs)
read File
final FileInputStream stream = new FileInputStream(file);
return readInputStream(stream, cs);
StringreadFile(File file, String charsetName)
read File
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (FileInputStream is = new FileInputStream(file)) {
    int length = 0;
    byte[] bytes = new byte[1024 * 4];
    while ((length = is.read(bytes)) > 0) {
        baos.write(bytes, 0, length);
} catch (FileNotFoundException e1) {
...
StringreadFile(File theFile, Charset theCharset)
read File
if (theCharset == null) {
    theCharset = Charset.defaultCharset();
Reader reader = new InputStreamReader(new FileInputStream(theFile), theCharset);
return readFromReaderIntoString(reader);