Android Utililty Methods File Read

List of utility methods to do File Read

Description

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

Method

Stringread(File path, int i)
Read file in line i
try {
    if (i == 1) {
        return new Scanner(path).nextLine();
    } else {
        for (int j = 1; j < i; j++) {
            new Scanner(path).nextLine();
        return new Scanner(path).nextLine();
...
StringreadFile(File file)
read File
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(file));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
    String readData = String.valueOf(buf, 0, numRead);
    fileData.append(readData);
    buf = new char[1024];
...
byte[]readFile(String filepath)
read File
byte[] result = null;
try {
    File file = new File(filepath);
    if (file.length() <= Integer.MAX_VALUE) {
        result = new byte[(int) file.length()];
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        raf.readFully(result);
} catch (FileNotFoundException e) {
} catch (IOException e) {
return result;
StringreadFileAsString(String filePath)
read File As String
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
String results = "";
while ((line = reader.readLine()) != null) {
    results += line;
reader.close();
return results;
...
byte[]readFileToByte(File file)
read File To Byte
ByteArrayOutputStream bos = new ByteArrayOutputStream(
        (int) file.length());
BufferedInputStream in = null;
try {
    in = new BufferedInputStream(new FileInputStream(file));
    int buf_size = 1024;
    byte[] buffer = new byte[buf_size];
    int len = 0;
...
StringreadFileToString(String fileName)
read File To String
FileInputStream fis = new FileInputStream(fileName);
String out = readInputStream(fis);
fis.close();
return out;
StringreadFileToString(String pathToFile)
read File To String
byte[] buffer = readFileToByteArray(pathToFile);
if (buffer != null) {
    return new String(buffer);
return null;
StringreadFromFile(String filename)
read From File
File file = new File(filename);
byte b[] = null;
try {
    if (file.exists()) {
        b = new byte[(int) file.length()];
        InputStream fi = new FileInputStream(file);
        fi.read(b);
        fi.close();
...
StringreadInstallationFile(File installation)
read Installation File
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
StringreadStringFromFile(File file)
read String From File
FileReader reader = null;
try {
    reader = new FileReader(file);
    StringBuffer stringBuffer = new StringBuffer();
    char[] buffer = new char[1024];
    int count = 0;
    while ((count = reader.read(buffer)) != -1) {
        stringBuffer.append(buffer, 0, count);
...