首先,我想道歉,如果这是一个简单的错误。我主要是自学成才,我所知道的是非常零星的,很可能是偶然的。文件未找到文件
我正在开发一个作为密码管理器的应用程序。它具有备份和恢复功能。我使用一个没有问题的固定目录工作,但在尝试添加文件选取器时,出现了问题。
保存文件位置没有问题。然后,在它确认它已保存后,它表示该文件不存在。这里是相关的代码以及错误日志。我只会将代码加入到崩溃中,接下来的内容是无关紧要的,但如果提问,可以提供。 PSync.psbk是试图读取的文件。坦率地说,不知道什么是错的,但我认为它很简单。
代码:
Toast.makeText(Main.this, "Method properly called.", Toast.LENGTH_SHORT).show();
//The file variable to imported.
File file;
try {
//Sets the file equal to the file found at the specified path.
//Used to access settings.
TinyDB database = new TinyDB(getApplicationContext());
String strfilePath = database.getString("FilePath");
Toast.makeText(Main.this, "Method properly called: " + strfilePath, Toast.LENGTH_SHORT).show();
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))) { //Line 776, as mentioned in err log.
错误日志:
W/System.err: java.io.FileNotFoundException: /document/storage/emulated/0/Download/PSync.psbk (No such file or directory)
10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err: at java.io.FileInputStream.open(Native Method)
10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:146)
10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err: at java.io.FileReader.<init>(FileReader.java:72)
10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err: at com.example.brand.psync.Main.onRequestPermissionsResult(Main.java:776)
你有没有给出清单 –
的许可是的我有,它也要求运行时权限。当文件格式为file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),“PSync.psbk”)时,读取文件没有问题。 – Prometheus
@NabinBhandari,是这段代码片段在onRequestPermissionsResult里面。代码只有在授予权限后才会运行。正如我告诉Amrutha,当我使用Environmemnt.get方法时,从相同的目录中读取同一个文件没有问题。 – Prometheus