Android,当读取存储的String字节数组时,它返回不同的字节数组值

时间:2015-04-24 14:05:04

标签: android string encryption bytearray

我正在尝试将String字节存储到一个文本文件中,它在我的电脑中工作得很好但是当我尝试将它实现到我的android项目中它存储但是当我想要获取字符串字节并将其转换为原始字节它不起作用。它再次在我的电脑上工作,我不知道它为什么不能在我的android项目上工作。

CODE:

try {
            String y = "Yyyyyyy";
            try {
                File file1 = new File(Environment.getExternalStorageDirectory() + File.separator + "test/test/newFile.txt");
                if (!file1.createNewFile()) {
                    EncryptedObject a = encryptedMessage.encrypt(y, "test", "test");
                    Log.e("BYTES FROM TEXT1", Arrays.toString(a.getEncryptedBytes()));
                    String example = new String(a.getEncryptedBytes());
                    Log.e("STRINGGGGG", example);
                    BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
                    buffer.write(example);
                    buffer.flush();
                    buffer.close();

                    BufferedReader readFile = new BufferedReader(new FileReader(file1));
                    String get_text = "";
                    String lines = null;

                    while ((lines = readFile.readLine()) != null) {
                        get_text += lines; // Gets each line
                    }
                    readFile.close();
                    //THE PROBLEM SEEMS TO START HERE
                    byte[] dec = get_text.getBytes(Charset.forName("UTF-8"));
                    Log.e("TEXT", get_text);
                    Log.e("BYTES FROM TEXT", Arrays.toString(dec));

                } else {
                    System.out.println("File Already Exist");

                }

            } catch (Exception e) {
                e.getCause();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

日志:

04-24 16:41:42.208    2934-2934/com.test.test E/ENCRYPTED TEXT﹕ ϩ{���
04-24 16:41:42.218    2934-2934/com.test.test E/ENCRYPTED TEXT﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218    2934-2934/com.test.test E/BYTES FROM TEXT1﹕ [-49, -87, 123, -15, 1, -84, -11]
04-24 16:41:42.218    2934-2934/com.test.test E/STRINGGGGG﹕ ϩ{���
04-24 16:41:42.218    2934-2934/com.test.test E/TEXT﹕ ϩ{���
04-24 16:41:42.218    2934-2934/com.test.test E/BYTES FROM TEXT﹕ [-49, -87, 123, -17, -65, -67, 1, -17, -65, -67, -17, -65, -67]

为什么返回不同的字节值?我感谢任何帮助,谢谢。 编辑。

2 个答案:

答案 0 :(得分:0)

file替换为{/ 1}}

file1

new BufferedWriter(new FileWriter(file1));

我的输出:

new BufferedReader(new FileReader(file1));
try {
    String y = "Yyyyyyy";
    byte[] a = y.getBytes(Charset.forName("UTF-8"));
    Log.e("TEXT", y);
    Log.e("BYTES FROM TEXT", Arrays.toString(a));

    try {
        File file1 = new File("test.txt");
        if (file1.createNewFile()) {
            String example = new String(a);

            BufferedWriter buffer = new BufferedWriter(new FileWriter(file1));
            buffer.write(example);
            buffer.flush();
            buffer.close();

            BufferedReader readFile= new BufferedReader(new FileReader(file1));
            String get_text = "";
            String lines = null;

            while ((lines = readFile.readLine()) != null){
                get_text += lines; // Gets each line
            }

            byte[] dec = get_text.getBytes(Charset.forName("UTF-8");
            Log.e("TEXT", get_text);
            Log.e("BYTES FROM TEXT", Arrays.toString(dec));
        } else {
            System.out.println("File Already Exist");
        }
    } catch(Exception e) {
        e.getCause();
    }
} catch(Exception e){
    e.getMessage();
}

答案 1 :(得分:0)

您的代码的主要错误是您将字符串误称为字节数组。 如果您正在编写Strings,请直接使用Writer编写它们。

然后您可以删除无用的转化,如下所示:

TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]
TEXT Yyyyyyy
BYTES FROM TEXT [89, 121, 121, 121, 121, 121, 121]

它将String转换为byte [],然后再转换回String。

Consclusion: 如果要指定用于保存字符串的字符集,请使用byte[] dec = get_text.getBytes(Charset.forName("UTF-8"); ,然后将结果以byte []形式写入OutputStream。

或者,当使用构造函数String.getBytes("UTF-8")

时,您可以直接编写字符串字符串。

类似的规则适用于阅读文件。