Java new File()表示FileNotFoundException但文件存在 [英] Java new File() says FileNotFoundException but file exists

查看:178
本文介绍了Java new File()表示FileNotFoundException但文件存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CS类的作业,它说要读取一个包含多个考试成绩的文件,并要求我对它们进行求和。虽然求和和平均很容易,但我在阅读文件方面遇到了问题。教练说使用这种语法

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"));

然而,这会引发FileNotFoundException,但我反复检查以查看文件是否存在在当前文件夹中,之后,我认为它必须对权限执行某些操作。我为每个人更改了读写权限,但它仍然无法正常工作,但仍然会抛出错误。有没有人知道为什么会发生这种情况?

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?

编辑:它实际上是指向一个目录,但是,我已经解决了这个问题。 file.exists()返回true,但是,当我尝试将它放入扫描程序时,它会抛出filenotfoundexception

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

这是我的所有代码

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);
        }
}


推荐答案

那里有三种情况可能会抛出 FileNotFoundException

There are three cases where a FileNotFoundException may be thrown.


  1. 指定的文件没有

  2. 指定的文件实际上是一个目录。

  3. 由于某种原因,无法打开指定的文件。

根据您的描述,前两种情况不太可能。我会使用 file.canRead()来测试第三种情况。

The first two cases are unlikely based on your description. I would test against the third case using file.canRead().

如果上面的测试返回true,我会怀疑以下内容:

If the test above returns true, I would suspect the following:

您可能忘记明确抛出或捕获潜在的异常(即 FileNotFoundExcetion ) 。如果您在IDE中工作,您应该收到编译器的一些投诉。但我怀疑你没有在这样的IDE中运行你的代码。

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.

我只是运行你的代码而没有照顾Netbeans的投诉,只是为了获得以下异常消息:

I've just run your code without taking care of the complaint from Netbeans, only to get the following exception message:


线程main中的异常java.lang.RuntimeException:无法编译的
源代码 - 未报告的异常java。 io.FileNotFoundException;必须
被捕获或声明被抛出

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);
}

这篇关于Java new File()表示FileNotFoundException但文件存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆