我有一个应用程序,可以创建其信息的备份并恢复它们。我正在研究第七个版本,并发现恢复方法不起作用。它表现得好像,但什么都不做。我无法弄清楚造成这种情况的原因,所以我决定从第六版开始。
我的所有版本都没有'尽管过去通过了所有测试,但恢复方法现在仍然有效。
它不会抛出错误或其他任何内容。它告诉用户"备份已经恢复。"祝酒。如果不允许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;
}
}
}
答案 0 :(得分:0)
问题与最初认为的完全不同,最终出现 许多 问题。主要是列出的。
我认为过去通过测试是出于人为错误,问题已经改变以反映出来。
这是更新的代码,功能齐全。
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);
}