【问题标题】:Broken Recovery Method破碎恢复方法
【发布时间】:2017-11-28 15:15:21
【问题描述】:

我有一个应用程序可以创建其信息的备份并恢复它们。我正在开发第七个版本,发现恢复方法不起作用。它表现得好像它做了,但什么也不做。我不知道是什么原因造成的,所以我决定从我的第六版重新开始。

尽管过去通过了所有测试,但我的版本的恢复方法现在都不起作用。

它不会抛出错误或其他任何东西。它告诉用户“备份已恢复”。祝酒。如果不允许 ext 存储,则会引发错误。如果文件路径不存在,则抛出错误。 logcat 上没有异常。但是,当一切正常时,它就是行不通的。所以...这是从稳定版本恢复的方法。如果您还有其他想要检查的内容,请告诉我。

编辑:更改了从 Base64 解码并将其设置为字符串的方法。最终结果还是一样。将问题缩小到 while 循环,它永远不会运行,因此实际上不会处理任何信息。

//Method025: Imports user acc settings from a file on a specified path.
public void importFile() {
    //The file variable to be imported.
    File file;

    try {
        //Used to access settings.
        TinyDB database = new TinyDB(getApplicationContext());

        //Sets the file equal to the file found at the specified path.
        String strfilePath = database.getString("FilePath");
        file = new File(strfilePath);

        //To be used to arrange the imported information.
        ArrayList<String> strAcc = new ArrayList<>();
        ArrayList<String> strUser = new ArrayList<>();
        ArrayList<String> strPass = new ArrayList<>();
        ArrayList<String> strAdditionalInfo = new ArrayList<>();

        //To be used to store all the information for additional info variables. This is
        //due to its multi-line nature requiring a slightly different method of
        //importation, the other variables are expected to be one line.
        String strExtraInfo = "";

        //Goes through the file and adds info to arrays for each corresponding variable.
        //If the line does not have an identifier, it assumes it to be an additional
        //info line, and will be processed later.
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;

            String strLine = br.readLine();
            //Decodes the line from Base64 and converts it to a string.
            byte[] decodedContent = Base64.decode(strLine.getBytes(), Base64.DEFAULT);
            strLine = new String (decodedContent);

            while ((line = br.readLine()) != null) {
                if (strLine.contains("[Acc]")) {
                    strLine = strLine.replace("[Acc]","");
                    strAcc.add(strLine);
                } else if (strLine.contains("[User]")) {
                    strLine = strLine.replace("[User]", "");
                    strUser.add(strLine);
                } else if (strLine.contains("[Pass]")) {
                    strLine = strLine.replace("[Pass]", "");
                    strPass.add(strLine);
                } else  {
                    strExtraInfo += strLine;
                }
            }
        }

【问题讨论】:

  • 放上整个代码并没有帮助。请在激活调试或至少打印跟踪的情况下执行此代码。
  • 会不会是备份文件本身损坏导致以前的版本无法加载?
  • @EleazarEnrique,对不起,你想要 logcat 吗?已经很晚了,还没睡,不知道你到底想要什么。 xs0 不,我尝试了多个备份/创建新备份。我已经在记事本中检查了文件本身,以确保它们是正确的格式等。
  • 嗯,在 while 循环中,您似乎阅读了 line,但随后在 if 语句中使用了 strLine..?
  • strLine 等于 br.readLine(),可能不需要变量,但它可以工作。我目前正在做我能想到的所有错误检查,我刚刚确认它正在使用 br.readLine()/strLine 从文件中获取文本,所以在读取文件后的某个时间会发生任何问题。当我缩小范围时会更新。

标签: java android file filereader


【解决方案1】:

问题与最初认为的完全不同,最终出现了许多问题。主要的如所列。

我认为过去通过测试是由于人为错误造成的,问题已更改以反映这一点。

  • Base64 解码方法不正确
  • 转换为 Base64 并返回混乱的行距
  • ReadLine() 只能被调用一次,调用越多什么也不返回
  • 没有足够的错误检查,例如 strAdditionalInfo.size > 0
  • 子字符串恢复方法会引发很多错误,使用 split() 可以更简单地完成

这是更新后的代码,功能齐全。

public void importFile() {
        //The file variable to be imported.
        File file;

        try {
            //Used to access settings.
            TinyDB database = new TinyDB(getApplicationContext());

            //Sets the file equal to the file found at the specified path.
            String strfilePath = database.getString("FilePath");
            file = new File(strfilePath);

            //To be used to arrange the imported information.
            ArrayList<String> strAcc = new ArrayList<>();
            ArrayList<String> strUser = new ArrayList<>();
            ArrayList<String> strPass = new ArrayList<>();
            ArrayList<String> strAdditionalInfo = new ArrayList<>();

            //To be used to store all the information for additional info variables. This is
            //due to its multi-line nature requiring a slightly different method of
            //importation, the other variables are expected to be one line.
            String strExtraInfo = "";

            //Goes through the file and adds info to arrays for each corresponding variable.
            //If the line does not have an identifier, it assumes it to be an additional
            //info line, and will be processed later.
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {

                String line;
                String strLine;

                while ((line = br.readLine()) != null) {
                    if (line.contains("[Acc]")) {
                        strLine = line.replace("[Acc]","");
                        strAcc.add(strLine);
                    } else if (line.contains("[User]")) {
                        strLine = line.replace("[User]", "");
                        strUser.add(strLine);
                    } else if (line.contains("[Pass]")) {
                        strLine = line.replace("[Pass]", "");
                        strPass.add(strLine);
                    } else  {
                        strExtraInfo += line;
                    }
                }
            }

            //Gets the list of accounts.
            ArrayList<String> savedInfo = new ArrayList<>(database.getListString("allSaved"));

            //To be used to get the AdditionalInfo variables one line at a time.
            String strSubInfo;

            //Gets rid of any erroneous spaces.
            while (strExtraInfo.contains("  ")) {
                strExtraInfo = strExtraInfo.replace("  ", " ");
            }

            Log.d("STRExtraInfo",strExtraInfo);
            strExtraInfo = strExtraInfo.replace("[ExtraStart]","");
            String array[] = strExtraInfo.split("\\[ExtraEnd\\]");
            ArrayList<String> strRawAdditionalInfo = new ArrayList<>();
            strRawAdditionalInfo = new ArrayList<>(Arrays.asList(array));

            for (String info : strRawAdditionalInfo){
                strAdditionalInfo.add(info);
                Log.d("ExtraInfo",info );
            }

            //Arranges the information.
            for (String name : strAcc) {
                savedInfo.add(name);

                ArrayList<String> allInfo = new ArrayList<>();

                //Gets the info then adds it to database.
                //Deletes the old information.
                if (strUser.size() > 0) {
                    allInfo.add(strUser.get(0));
                    strUser.remove(0);
                }

                if (strPass.size() > 0) {
                    allInfo.add(strPass.get(0));
                    strPass.remove(0);
                }

                if (strAdditionalInfo.size() > 0) {
                    allInfo.add(strAdditionalInfo.get(0));
                    strAdditionalInfo.remove(0);
                }
                database.putListString(name,allInfo);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多