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

查看:31
本文介绍了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:

  • 路径名完全错误
  • 路径名看起来正确但实际上是错误的,因为它包含您没有注意到的非打印字符(或同形文字)
  • 路径名是相对的,相对于正在运行的应用程序的实际当前目录,它不能正确解析.这通常是因为应用程序的当前目录不是您期望或假设的目录.
  • 文件路径损坏;例如路径的目录名称不正确,路径上的符号链接已损坏,或者路径组件之一存在权限问题.
  • 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 named file cannot be opened for reading for some reason.

好消息是,问题将不可避免地成为上述问题之一.这只是一个解决问题的问题.以下是您可以尝试的一些方法:

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() 将告诉您是否存在具有给定名称/路径名的任何文件系统对象.

  • Calling file.exists() will tell you if any file system object exists with the given name / pathname.

调用 file.isDirectory() 会测试它是否是一个目录.

Calling file.isDirectory() will test if it is a directory.

调用 file.canRead() 将测试它是否是可读文件.

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 trailing 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:

    线程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

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

    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天全站免登陆