破损恢复法
Broken Recovery Method
我有一个应用程序可以创建其信息的备份并恢复它们。我正在开发第七个版本,发现恢复方法不起作用。它表现得好像在做,但什么也不做。我不知道是什么原因造成的,所以我决定从我的第六个版本开始。
None 我的版本的恢复方法现在有效,尽管过去通过了所有测试。
它不会抛出错误或其他任何东西。它告诉用户 "The backup has been restored." 干杯。如果不允许 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;
}
}
}
这个问题和一开始认为的完全不一样,结果出现了很多个问题。主要的如表所示。
我认为过去通过测试是由于人为错误,问题已更改以反映这一点。
- Base64解码方法不正确
- 转换为 Base64 并返回乱行间距
- ReadLine()只能被调用一次,多做几次returns没什么
- 对 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);
}
我有一个应用程序可以创建其信息的备份并恢复它们。我正在开发第七个版本,发现恢复方法不起作用。它表现得好像在做,但什么也不做。我不知道是什么原因造成的,所以我决定从我的第六个版本开始。
None 我的版本的恢复方法现在有效,尽管过去通过了所有测试。
它不会抛出错误或其他任何东西。它告诉用户 "The backup has been restored." 干杯。如果不允许 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;
}
}
}
这个问题和一开始认为的完全不一样,结果出现了很多个问题。主要的如表所示。
我认为过去通过测试是由于人为错误,问题已更改以反映这一点。
- Base64解码方法不正确
- 转换为 Base64 并返回乱行间距
- ReadLine()只能被调用一次,多做几次returns没什么
- 对 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);
}