android - 文件从字符串中读取换行符,而字符串上没有换行符

标签 android file android-file

下图是写在文件里的, enter image description here

这是写入文件的代码。这里使用的 SecurityUtils 类是将字符串编码为 AES/CBC。

public static boolean reWriteToFile(String str1,
            String str2, String str3, String str4) {

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            SecurityUtils securityUtils = new SecurityUtils();
            String st = securityUtils.getEncryptedToken(str1) + "\n" + securityUtils.getEncryptedToken(str2) + "\n" + securityUtils.getEncryptedToken(str3) + "\n" + securityUtils.getEncryptedToken(str4);

            Logger.i("STRING_TO_WRITE", st);

            File externalStorageDir = Environment.getExternalStorageDirectory();
            File myFile = new File(externalStorageDir, APP_CONFIG_FILE_NAME);

            try {
                FileOutputStream fOut1 = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut1);
                myOutWriter.append(st);
                myOutWriter.close();
                fOut1.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return true;
        } else {
            return false;
        }

    }

这是从文件中读取的代码:

public static boolean readFromFile() {
        SecurityUtils securityUtils = new SecurityUtils();

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File externalStorageDir = Environment.getExternalStorageDirectory();
            File myFile = new File(externalStorageDir, APP_CONFIG_FILE_NAME);
            if (myFile.exists()) {

                try {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(new FileInputStream(myFile)));

                    String data;
                    StringBuffer sb = new StringBuffer();
                    while ((data = br.readLine()) != null) {
                        sb.append(data + ",");
                    }

                    String str = sb.toString().replace(",,", ",");
                    String[] both = str.split(",");
                    Log.d("3rd string", both[3]);


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            return false;

        } else
            return false;
    }

但在 both[3] 的日志中,我只得到第 5 行,如图所示,我需要第 5 到第 9 行,而 第一行是一个没有换行符的字符串 第三个也是没有换行符的字符串 第 5-9 也是一个没有换行符的字符串 第 11 个也是没有换行符的字符串。 但是在每个不同的字符串之间有一个换行符

这些都是AES/CBC编码的字符串 不是全部。

最佳答案

您的问题的解决方案非常简单 - 您不应该向文件写入任何字符串,它们只是加密的结果。每次,当你想加密一些字符串时,加密的结果应该用Base64编码。使用以下代码对您的加密结果进行编码。

public String base64Encoding(String input) throws UnsupportedEncodingException {

    String base64 = "";

    byte[] hex = input.getBytes("utf8");
    base64 = Base64.encodeToString(hex, Base64.NO_WRAP);

    return base64;
}

你的代码应该是这样的

String st = base64Encoding(securityUtils.getEncryptedToken(str1)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str2)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str3)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str4));

将它粘贴到您的 try catch block 中,一切都应该没问题。
当你想读取你的文件,并解密你的字符串时,你应该先解码base64,然后再解密aes。这是示例代码

public String base64ToText(String input) throws UnsupportedEncodingException {

    String text = "";

    byte[] data = Base64.decode(input, Base64.NO_WRAP);
    text = new String(data, "utf8");

    return text;
}

关于android - 文件从字符串中读取换行符,而字符串上没有换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276848/

相关文章:

Android:无法取消重复闹钟

java - 如何打开网络上的 txt 文件并将其内容读取为字符串?

安卓选择mp3文件

android - 如何在 android aidl 文件中返回 MyObject 列表?

Android TabHost 和 SearchView 焦点问题

java - 滚动到 ListPreference 中的选定项目

android - 如何在 Android 中打开 .crt 文件?

c++ - 从文件中读取和存储数据时,每隔一行数据都会被跳过

java - 获取Android中任何文件的真实文件路径

android - 如何将 PdfDocument 对象保存到 Android 中的文件中?