2013-02-01 25 views
1

我想將一些字符串存儲在一個簡單的.txt文件中,然後讀取它們,但是當我想使用Base64對它們進行編碼時,它不再起作用了:它寫得很好,但閱讀不起作用。 ^^如何讀取/寫入用android.util.Base64編碼的字符串

寫方法:

private void write() throws IOException { 
    String fileName = "/mnt/sdcard/test.txt"; 
    File myFile = new File(fileName); 

    BufferedWriter bW = new BufferedWriter(new FileWriter(myFile, true)); 

    // Write the string to the file 
    String test = "http://google.fr"; 
    test = Base64.encodeToString(test.getBytes(), Base64.DEFAULT); 

    bW.write("here it comes"); 
    bW.write(";"); 
    bW.write(test); 
    bW.write(";"); 

    bW.write("done"); 
    bW.write("\r\n"); 

    // save and close 
    bW.flush(); 
    bW.close(); 
} 

read方法:

private void read() throws IOException { 
    String fileName = "/mnt/sdcard/test.txt"; 
    File myFile = new File(fileName); 
    FileInputStream fIn = new FileInputStream(myFile); 

    BufferedReader inBuff = new BufferedReader(new InputStreamReader(fIn)); 
    String line = inBuff.readLine(); 
    int i = 0; 
    ArrayList<List<String>> matrice_full = new ArrayList<List<String>>(); 
    while (line != null) { 
     matrice_full.add(new ArrayList<String>()); 
     String[] tokens = line.split(";"); 

     String decode = tokens[1]; 
     decode = new String(Base64.decode(decode, Base64.DEFAULT)); 

     matrice_full.get(i).add(tokens[0]); 
     matrice_full.get(i).add(tokens[1]); 
     matrice_full.get(i).add(tokens[2]); 
     line = inBuff.readLine(); 
     i++; 
    } 
    inBuff.close(); 
} 

任何想法,爲什麼?

回答

4

您的代碼中有幾個錯誤。

首先一對夫婦對你的代碼說明:

  1. 在這裏發帖,附加SSCCE幫助別人調試代碼。這不是SSCEE,因爲它不能編譯。它缺少幾個定義的變量,所以你必須猜測你的真正含義。您的代碼中還粘貼了密切評論標記:*/,但沒有一個開始評論標記。
  2. 捕捉和只是抑制異常(如在read方法catch-block)是真的很糟糕想法,除非你真的知道你在做什麼。大部分時間它的作用是隱藏你潛在的問題。至少寫入異常的堆棧跟蹤是一個catch塊。
  3. 爲什麼你不只是調試它,檢查到目的文件的輸出到底是什麼?你應該學會如何做到這一點,因爲這會加快你的開發過程,特別是對於難以捕捉問題的大型項目。

回到解決方案:

  1. 運行程序。它拋出一個異常:

    02-01 17:18:58.171: E/AndroidRuntime(24417): Caused by: java.lang.ArrayIndexOutOfBoundsException 
    

    造成此行:

    matrice_full.get(i).add(tokens[2]); 
    

    檢查變量tokens顯示,其2元素,3

  2. 因此讓我們打開write方法生成的文件。這樣做會顯示此輸出:

    here it comes;aHR0cDovL2dvb2dsZS5mcg== 
    ;done 
    here it comes;aHR0cDovL2dvb2dsZS5mcg== 
    ;done 
    here it comes;aHR0cDovL2dvb2dsZS5mcg== 
    ;done 
    

    注意行在這裏。這是因爲Base64.encodeToString()在編碼字符串的末尾追加了新行。要生成一個單一的線路,而無需額外的新行,添加Base64.NO_WRAP因爲這樣的第二個參數:

    test = Base64.encodeToString(test.getBytes(), Base64.NO_WRAP); 
    

    注意這裏,你必須刪除之前創建,因爲它具有不正確的斷行文件。

  3. 再次運行代碼。現在創建一個具有正確內容的文件:

    here it comes;aHR0cDovL2dvb2dsZS5mcg==;done 
    here it comes;aHR0cDovL2dvb2dsZS5mcg==;done 
    
  4. 印刷matrice_full輸出現在給:

    [ 
        [here it comes, aHR0cDovL2dvb2dsZS5mcg==, done], 
        [here it comes, aHR0cDovL2dvb2dsZS5mcg==, done] 
    ] 
    

    請注意,你不會在你的代碼做與任何有價值的東西在decode變量,因此第二個元素是從該文件讀取的該值的Base64表示。

+0

也非常感謝您的建議:) 完整的代碼比長3倍,所以我試圖簡化它,也許有點太快了:P 好吧,我沒有看到的Base64加當我用記事本在windows上打開我的文件時出現了斷行,我不得不使用notepad ++來查看它。 但即使是Base64.NO_WRAP和test.trim(),它仍然會添加額外的分隔線。你知道爲什麼嗎 ? –

+0

它沒有 - 我通過在真實設備上運行代碼進行檢查。假設你已經刪除了舊文件內容,那麼必須添加額外的換行符。 – andr

+0

對不起Base64.NO_WRAP很棒:D –

相關問題