Android Utililty Methods Text File Read

List of utility methods to do Text File Read

Description

The list of methods to do Text File Read are organized into topic(s).

Method

String[]readLines(String file)
Generic read line method implementation from a file.
String line;
fr = new FileReader(file);
lines = new ArrayList<>();
lr = new LineNumberReader(fr);
while ((line = lr.readLine()) != null) {
    if (!line.startsWith("#") && !line.isEmpty()) {
        lines.add(line);
fr.close();
lr.close();
String[] ret = lines.toArray(new String[0]);
lines.clear();
lines = null;
fr = null;
lr = null;
return ret;
StringreadStringFromClasspath(String path, Class c)
Reads the entire InputStream and returns its content as a single String
final InputStream is = c.getResourceAsStream(path);
return readStringFromBufferedReader(createBufferedUtf8Reader(is));
StringreadStringFromFile(String fileName)
Reads the entire file and returns its content as a single String
return readStringFromBufferedReader(createBufferedUtf8Reader(fileName));
StringreadText(String path)
read Text
InputStream resourceAsStream = FileUtil.class
        .getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(
        resourceAsStream, Charset.forName("UTF-8")));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null) {
    sb.append(line).append("\n");
...
StringreadTextFile(String filePath)
read Text File
File textFile = new File(filePath);
BufferedReader in = null;
String text = null;
StringBuilder allText = new StringBuilder("");
try {
    in = new BufferedReader(new FileReader(textFile));
    while ((text = in.readLine()) != null) {
        allText.append(text).append("\n");
...
StringreadTextFile(String path)
Reads the contents of a text file into a String.
StringBuilder contents = new StringBuilder();
String lineSep = System.getProperty("line.separator");
BufferedReader input = new BufferedReader(new FileReader(path));
try {
    String line;
    while ((line = input.readLine()) != null) {
        contents.append(line);
        contents.append(lineSep);
...
voidsetFileResourceText(String path, String content)
set File Resource Text
BufferedWriter fileWriter = null;
try {
    fileWriter = new BufferedWriter(new FileWriter(path));
    fileWriter.write(content);
} finally {
    if (fileWriter != null) {
        fileWriter.close();
StringgetContent(String file, String encodType)
get Content
StringBuffer content = new StringBuffer();
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis,
        encodType));
String line = null;
if ((line = br.readLine()) != null) {
    content.append(line);
...
StringgetString(File file)
Get string from file
if (file == null)
    throw new NullPointerException("null file");
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
    sb.append(line);
    sb.append('\n');
...
StringreadFileSdcard(File file)
read File Sdcard
String res = "";
FileInputStream fin = new FileInputStream(file);
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
return res;
...