可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have an assignment for my CS class where it says to read a file with several test scores and asks me to sum and average them. While summing and averaging is easy, I am having problems with the file reading. The instructor said to use this syntax
Scanner scores=new Scanner(new File(\"scores.dat\"));
However, this throws a FileNotFoundException, but I have checked over and over again to see if the file exists in the current folder, and after that, I figured that it had to do something with the permissions. I changed the permissions for read and write for everyone, but it still did not work and it still keeps throwing the error. Does anyone have any idea why this may be occurring?
EDIT: It was actually pointing to a directory up, however, I have fixed that problem. file.exists() returns true, however, when I try to put it in the scanner, it throws the filenotfoundexception
Here is all my code
import java.util.Scanner;
import java.io.*;
public class readInt{
public static void main(String args[]){
File file=new File(\"lines.txt\");
System.out.println(file.exists());
Scanner scan=new Scanner(file);
}
}
回答1:
There are three cases where a FileNotFoundException
may be thrown.
- The named file does not exist.
- The named file is actually a directory.
- The named file cannot be opened for reading for some reason.
The first two cases are unlikely based on your description. I would test against the third case using file.canRead()
.
If the test above returns true, I would suspect the following:
You might have forgotten to explicitly throw or catch the potential exception (i.e., FileNotFoundExcetion
). If you work in an IDE, you should have got some complaint from the compiler. But I suspect you didn\'t run your code in such an IDE.
I\'ve just run your code without taking care of the complaint from Netbeans, only to get the following exception message:
Exception in thread \"main\" java.lang.RuntimeException: Uncompilable
source code - unreported exception java.io.FileNotFoundException; must
be caught or declared to be thrown
Try the following code and see if the exception would be gone:
public static void main(String[] args) throws FileNotFoundException {
File file=new File(\"scores.dat\");
System.out.println(file.exists());
Scanner scan=new Scanner(file);
}
回答2:
The code itself is working correctly. The problem is, that the program working path is pointing to other place than you think.
Use this line and see where the path is:
System.out.println(new File(\".\").getAbsoluteFile());
回答3:
Obviously there are a number of possible causes and the previous answers document them well, but here\'s how I solved this for in one particular case:
A student of mine had this problem and I nearly tore my hair out trying to figure it out. It turned out that the file didn\'t exist, even though it looked like it did. The problem was that Windows 7 was configured to \"Hide file extensions for known file types.\" This means that if file appears to have the name \"data.txt\" its actual filename is \"data.txt.txt\".
Hope this helps others save themselves some hair.
回答4:
I recently found interesting case that produces FileNotFoundExeption when file is obviously exists on the disk.
In my program I read file path from another text file and create File object:
//String path was read from file
System.out.println(path); //file with exactly same visible path exists on disk
File file = new File(path);
System.out.println(file.exists()); //false
System.out.println(file.canRead()); //false
FileInputStream fis = new FileInputStream(file); // FileNotFoundExeption
The cause of story is that path contains invisible \\r\\n symbols at the end.
Fix:
File file = new File(path.trim());
回答5:
Reading and writing from and to a file can be blocked by your OS depending on the file\'s permission attributes.
If you are trying to read from the file, then I recommend using File\'s setReadable method to set it to true, or, this code for instance:
String arbitrary_path = \"C:/Users/Username/Blah.txt\";
byte[] data_of_file;
File f = new File(arbitrary_path);
f.setReadable(true);
data_of_file = Files.readAllBytes(f);
f.setReadable(false); // do this if you want to prevent un-knowledgeable
//programmers from accessing your file.
If you are trying to write to the file, then I recommend using File\'s setWritable method to set it to true, or, this code for instance:
String arbitrary_path = \"C:/Users/Username/Blah.txt\";
byte[] data_of_file = { (byte) 0x00, (byte) 0xFF, (byte) 0xEE };
File f = new File(arbitrary_path);
f.setWritable(true);
Files.write(f, byte_array);
f.setWritable(false); // do this if you want to prevent un-knowledgeable
//programmers from changing your file (for security.)
回答6:
Apart from all the other answers mentioned here, you can do one thing which worked for me.
If you are reading the path through Scanner or through command line args, instead of copy pasting the path directly from Windows Explorer just manually type in the path.
It worked for me, hope it helps someone :)
回答7:
An easy fix, which worked for me, is moving my files out of src and into the main folder of the project. It\'s not the best solution, but depending on the magnitude of the project and your time, it might be just perfect.