【发布时间】: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