Mueyiwa Moses Ikomi Mueyiwa Moses Ikomi - 1 month ago 8
Android Question

How to read base64 String from Random Access File (Android)

i have a random access file that contains a base64 string

AwFmIY4A+f6ABoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAgAAAAAAAAAAAAAAAAAAAAA1i9NeX4wQXmkQq94Wl1beNBlUPkQkUv5frOi+PDDT3iWxqt4ss6reM7iUfiM7Kv5KO5NeFLyV/j2+6Z4bv2r+GYiX/z4KJ78fC5ZfPBHonysiVP86qamfZB5nPDujE3wypSocPy0T3GXD57xqHtH9K7xUfWS7ElprvJDybEDROmc+53IAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMBZiGMAP/+gAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAIAAAAAAAAAAAAAAAAAAAAATpQoficZVv5EmxP+bS+o3kA6FJ4vvCt+IL2WnlW9095vvpL+KEBrPkjAqh4XC8H/b4yQNxsgrH86pBS/VSTS30mraf8xiNkUSyUTfEInKhxJstScM7MrPCkKmJ1Fi9KdTYvn/S+Nlp06NGtdOD2UvVacEzJSnSmyTS9UWhMwAtIYMRfTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=


The problem is that when i read it using this code, i can't seem to return the exact thing that is in the text file.

File f = new File("fprint.txt");
RandomAccessFile randomFile = new RandomAccessFile(f, "rw");
String fromFile = randomFile.readLine();


When i log "fromFile", i get

AwFmIY4A+f6ABoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAgAAAAAAAAAAAAAAAAAAAAA1


Which is way shorter than what i put in. I want to able to read everything in the file the exact same way. Thank you

Edited: log code

Log.i("Check File", "Content of File: " + fromFile );

Answer

You should use read() method, instead of readLine(), for example:

public static String getBase64FromFile(String path) throws IOException {
    RandomAccessFile file = new RandomAccessFile(path, "r");
    String result = "";
    int b = file.read();
    while (b != -1) {
        result = result + (char) b;
        b = file.read();
    }
    file.close();

    return result;
}