android - 如何读取/写入使用 android.util.Base64 编码的字符串

标签 android io base64 fileinputstream fileoutputstream

我想将一些字符串存储在一个简单的 .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();
}

有什么想法吗?

最佳答案

您的代码中有几个错误。

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

  1. 在这里发帖时,附上一个SSCCE帮助其他人调试您的代码。这不是 SSCEE,因为它不编译。它缺少几个已定义的变量,因此必须猜测您的真正意思。此外,您还在代码中粘贴了关闭评论标记:*/ 但没有一个开始评论标记。
  2. 捕捉并抑制异常(例如 read 方法中的 catch block )是非常糟糕的主意,除非您真的知道自己在做什么。它大部分时间所做的是向您隐藏潜在的问题。至少写入异常的堆栈跟踪是一个 catch block 。
  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

    /li>
  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 表示。

关于android - 如何读取/写入使用 android.util.Base64 编码的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649780/

相关文章:

android - Firebase 统计数据

java - 创建更新程序

.net - Haskell 或 F# 高吞吐量二进制 I/O

ios使用des ecb解密base64编码的字符串

javascript - 不知道图片类型时的Base64图片SRC

c# - Base64 在 SQL 中编码一个 utf8 字符串

java - 静态公共(public) boolean 值与公共(public)静态 boolean 值

php - Android 和 JavaScript : How to know in javascript that an application is installed in the Android device or not?

android - Systrace 输出错误

java - 从桌面运行 JAR 时出现问题,但从命令行或 Eclipse 运行时不出现问题