2016-11-01 88 views
1

我有一個包含一個base64字符串隨機訪問文件的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 

這是比我投入的方式更短。我希望能夠讀取所有文件中的完全相同的方式。謝謝

編輯:日誌,而不是readLine()代碼

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

你能告訴確切的代碼登錄所用的Base64編碼文件? –

+0

剛剛添加了@TimBiegeleisen –

+0

嘗試使用readFully而不是readLine – Blackbelt

回答

1

您應該使用read()方法,例如:

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

謝謝謝爾蓋,工作就像一個魅力。正是我需要的...... –