Java说FileNotFoundException但文件存在 [英] Java says FileNotFoundException but file exists

查看:1091
本文介绍了Java说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,但是当我尝试将其放入Scanner时,它会抛出FileNotFoundException

It was actually pointing to a directory up, however, I have fixed that problem. Now file.exists() returns true, but 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 a number situation where a FileNotFoundException may be thrown at runtime.

  1. 命名文件不存在.这可能是出于多种原因,包括:

  1. The named file does not exist. This could be for a number of reasons including:

  • 路径名完全错误
  • 该路径名看起来正确,但实际上是错误的,因为它包含您没有注意到的非打印字符(或同形文字)
  • 路径名是相对的,相对于正在运行的应用程序的 actual 当前目录,它无法正确解析.通常发生这种情况是因为应用程序的当前目录不是您期望或假定的目录.
  • 文件路径已损坏;例如路径的目录名称不正确,路径上的符号链接断开或路径组件之一存在权限问题.
  • The pathname is simply wrong
  • The pathname looks correct but is actually wrong because it contains non-printing characters (or homoglyphs) that you did not notice
  • The pathname is relative, and it doesn't resolve correctly relative to the actual current directory of the running application. This typically happens because the application's current directory is not what you are expecting or assuming.
  • The path to the file is is broken; e.g. a directory name of the path is incorrect, a symbolic link on the path is broken, or there is a permission problem with one of the path components.

命名文件实际上是一个目录.

The named file is actually a directory.

好消息,问题将不可避免地是上述之一.只是确定哪一个问题.您可以尝试以下操作:

The good news that, the problem will inevitably be one of the above. It is just a matter of working out which. Here are some things that you can try:

  • 调用file.exists()会告诉您是否存在具有给定名称/路径名的文件系统对象.
  • 呼叫file.isDirectory()将测试它是否是目录.
  • 调用file.canRead()将测试它是否是可读文件.
  • 此行将告诉您当前目录是什么:

  • Calling file.exists() will tell you if any file system object exists with the given name / pathname.
  • Calling file.isDirectory() will test if it is a directory.
  • Calling file.canRead() will test if it is a readable file.
  • This line will tell you what the current directory is:

System.out.println(new File(".").getAbsolutePath());

  • 此行将以某种方式打印出路径名,从而更容易发现意外的前导或训练式空白:

  • This line will print out the pathname in a way that makes it easier to spot things like unexpected leading or trainiong whitespace:

    System.out.println("The path is '" + path + "'");
    

    在输出中查找意外的空格,换行符等.

    Look for unexpected spaces, line breaks, etc in the output.

    事实证明您的示例代码有编译错误.

    It turns out that your example code has a compilation error.

    我运行您的代码时并没有处理Netbeans的投诉,只是得到以下异常消息:

    I ran your code without taking care of the complaint from Netbeans, only to get the following exception message:

    线程主"中的异常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

    如果将代码更改为以下代码,它将解决那个问题.

    If you change your code to the following, it will fix that problem.

    public static void main(String[] args) throws FileNotFoundException {    
        File file = new File("scores.dat");
        System.out.println(file.exists());
        Scanner scan = new Scanner(file);
    }
    

    说明:Scanner(File)构造函数声明为抛出FileNotFoundException异常. (碰巧扫描程序无法打开文件.)现在FileNotFoundException已检查的异常.这意味着可能抛出异常 的方法必须捕获异常或在throws子句中声明该异常.上面的修复程序采用了后一种方法.

    Explanation: the Scanner(File) constructor is declared as throwing the FileNotFoundException exception. (It happens the scanner it cannot open the file.) Now FileNotFoundException is a checked exception. That means that a method in which the exception may be thrown must either catch the exception or declare it in the throws clause. The above fix takes the latter approach.

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

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