如何从随机访问文件(Android)中读取base64字符串

时间:2016-11-01 09:20:39

标签: java android randomaccessfile

我有一个包含base64字符串的随机访问文件

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

问题在于,当我使用此代码阅读它时,我似乎无法返回文本文件中的确切内容。

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

当我记录“fromFile”时,我得到了

AwFmIY4A+f6ABoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAgAAAAAAAAAAAAAAAAAAAAA1

这比我输入的更短。我希望能够以完全相同的方式读取文件中的所有内容。谢谢

已编辑:日志代码

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

1 个答案:

答案 0 :(得分:1)

您应该使用read()方法,而不是readLine(),例如:

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;
}