简体   繁体   English

文件存在于与程序相同的文件夹中的FileNotFoundException

[英]FileNotFoundException while file exists in same folder as program

Similar questions deal with files on the C: drive, where hardcoding the file path is an acceptable answer. 类似的问题涉及C:驱动器上的文件,其中硬编码文件路径是可接受的答案。 This application is mobile, and hardcoding the file path is not practical. 此应用程序是移动的,硬编码文件路径是不实际的。

I am trying to import a text file via scanner, that contains a list of strings, 15 characters each, 1 per line. 我试图通过扫描仪导入一个文本文件,其中包含一个字符串列表,每个字符串15个字符,每行1个字符。 LOTS of lines. 很多行。 The file name is a.txt . 文件名是a.txt

I call it using 我叫它用

File data = new File("a.txt");
Scanner in = new Scanner(data);

repeated the lines below with "b.txt", using different Object names. 用“b.txt”重复下面的行,使用不同的Object名称。

However, when I build the program, I get this error log (using Jcreator IDE): 但是,当我构建程序时,我得到了这个错误日志(使用Jcreator IDE):

--------------------Configuration: <Default>--------------------
E:\Simple Encryption\Simple_Encryption.java:17: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    Scanner in = new Scanner(data);
                 ^
E:\Simple Encryption\Simple_Encryption.java:18: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    Scanner numsIn = new Scanner(nums);
                     ^
2 errors

Process completed.

The program is in the folder E:/Simple Encryption/ , which contains the following files and folders: 该程序位于文件夹E:/Simple Encryption/ ,其中包含以下文件和文件夹:

  • Simple_Encryption.java Simple_Encryption.java
  • Simple_Encryption.class Simple_Encryption.class
  • lock.png (unused) lock.png(未使用)
  • a.txt A.TXT
  • b.txt (same error as a.txt on import, just one line below) b.txt(导入时与a.txt相同的错误,下面只有一行)
  • /images (folder, contains icon.png) / images(folder,contains icon.png)

There is nothing else in the folder. 文件夹中没有其他内容。 I cannot hardcode the file path because on different computers, it has different drive names, and will be distributed under different folders later. 我无法对文件路径进行硬编码,因为在不同的计算机上,它具有不同的驱动器名称,稍后将在不同的文件夹下分发。 (Here (School), it's on the E: drive, at home it's the J: drive, and at work it's on the G: drive.) (这里(学校),它在E:驱动器上,在家里它是J:驱动器,在工作时它在G:驱动器上。)

Although I have gotten help from existing errors, I need to import this on the program startup, and not just skip it. 虽然我从现有错误中得到了帮助,但我需要在程序启动时导入它,而不是仅仅跳过它。 The file exists in it's current form, and the program cannot function without it. 该文件以当前形式存在,如果没有它,程序将无法运行。

What is causing this error? 导致此错误的原因是什么? And what can I do to prevent this? 我该怎么做才能防止这种情况发生?

E:\Simple Encryption\Simple_Encryption.java:17: unreported exception 
java.io.FileNotFoundException; must be caught or declared to be thrown

As your exception saying you need to use try- catch block 作为您的例外,您需要使用try-catch块

try {
   Scanner numsIn = new Scanner(nums);
catch (FileNotFoundException e) {
  // do something
} finally {
  if (numsIn != null) numsIn .close();
}

OR 要么

you need to throw that exception though calling method 你需要通过调用方法抛出异常

public static void xxxx() throws FileNotFoundException {
   // do something
}

The problem is not the exception that is thrown, but the fact that the constuctor is declared as throwing the FileNotFoundException checked exception. 问题不是引发的异常,而是构造函数被声明为抛出FileNotFoundException检查异常的事实。

public Scanner(File source) throws FileNotFoundException

Since it is a checked exception it must be handled by the programmer, because it is used to signal something that may happen. 由于它是一个经过检查的异常,因此它必须由程序员处理,因为它用于表示可能发生的事情。

So you must enclose them in a try/catch statement: 所以你必须将它们包含在try/catch语句中:

Scanner in = null;
try {
  in = new Scanner(file);
}
catch (FileNotFoundException e) {
  e.printStackTrace();
}

As you say the file exists, but it states that it cannot find the file. 正如您所说的文件存在,但它声明它无法找到该文件。 Try this: 尝试这个:

System.out.println(data.getAbsolutePath());

This should let you know exactly where it is looking for the file, and you can then tweak the app without using full paths 这可以让您确切地知道它在哪里查找文件,然后您可以在不使用完整路径的情况下调整应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM