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

voidreadToBuffer(StringBuffer buffer, String filePath, Charset charset)
read To Buffer
InputStream is = new FileInputStream(filePath);
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
line = reader.readLine();
while (line != null) {
    buffer.append(line);
    buffer.append("\n");
    line = reader.readLine();
...
StringreadToString(InputStream in, Charset charset)
Read the input stream into a string
return readToString(in, 1024, charset);
StringreadToString(ReadableByteChannel in, Charset charset)
read To String
StringBuilder builder = new StringBuilder();
ByteBuffer buffer = ByteBuffer.allocate(2048);
while (in.read(buffer) != -1) {
    builder.append(new String(buffer.array(), 0, buffer.position(), charset));
    buffer.flip();
return builder.toString();
StringreadURL(URL url, Charset encoding)
Read from a url and returns a String of the contents.
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
String line;
StringBuilder sb = new StringBuilder();
try {
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
    while ((line = in.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
...
StringreadWholeFile(String filename, String charSet)
Reads whole file in and returns it as a string, even if file is binary.
FileInputStream ins = new FileInputStream(filename);
String contents = null;
try {
    contents = readWholeFile(ins, Charset.forName(charSet));
} finally {
    try {
        ins.close();
    } catch (Exception ex) {
...