【问题标题】:How to read/write a string encoded with android.util.Base64如何读取/写入使用 android.util.Base64 编码的字符串
【发布时间】:2013-02-01 15:37:52
【问题描述】:

我想将一些字符串存储在一个简单的 .txt 文件中,然后读取它们,但是当我想使用 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();
}

读取方法:

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

有什么想法吗?

【问题讨论】:

    标签: android io base64 fileinputstream fileoutputstream


    【解决方案1】:

    您的代码中有几个错误。

    首先对您的代码做几点说明:

    1. 在此处发布时,附上SSCCE 有助于其他人调试您的代码。这不是 SSCEE,因为它不能编译。它缺少几个定义的变量,所以你必须猜出你的真正意思。此外,您在代码中粘贴了关闭评论标记:*/,但没有一个开始评论标记。
    2. 除非您真的知道自己在做什么,否则捕获并仅抑制异常(例如在read 方法中的catch-block 中)是非常糟糕的 想法。它大部分时间所做的就是向您隐藏潜在的问题。至少写一个异常的stacktrace是一个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 表示。

    【讨论】:

    • 非常感谢您的建议 :) 完整的代码比那长 3 倍,所以我尝试简化它,可能有点太快了 :p 好的,我没有看到 Base64 添加了当我用记事本在 Windows 上打开文件时换行,我必须使用 notepad++ 才能看到它。但即使使用 Base64.NO_WRAP 和 test.trim() ,它也会不断添加额外的断线。你知道为什么吗?
    • 它没有 - 我已经通过在真实设备上运行代码进行了检查。假设您已经删除了旧文件内容,那么肯定还有其他内容添加了额外的换行符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多