List of utility methods to do Text File Read by Charset
void | readToBuffer(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(); ... |
String | readToString(InputStream in, Charset charset) Read the input stream into a string return readToString(in, 1024, charset);
|
String | readToString(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(); |
String | readURL(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"); ... |
String | readWholeFile(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) { ... |