我的java代码有缺陷,但我不明白为什么

发布于 2024-10-26 22:33:42 字数 1226 浏览 5 评论 0原文

我对java很陌生,我缺少一些非常基本的东西。当我运行代码时,我试图为代码中创建的帐户添加价值。当我尝试运行代码时,我收到一条错误,指出找不到文件,但我认为该文件是在代码内创建的。

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class DoPayroll
{
public static void main(String args[])
                                    throws
        IOException     
{
    Scanner diskScanner =
        new Scanner(new File("EmployeeInfo.txt"));

    for (int empNum = 1; empNum <= 3; empNum++)
    {
        payOneEmployee(diskScanner);
    }
}

static void payOneEmployee(Scanner aScanner)
{
    Employee anEmployee = new Employee();

    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    anEmployee.cutCheck(aScanner.nextDouble());
    aScanner.nextLine();
}
}

一旦运行,我收到以下错误

Exception in thread "main" java.io.FileNotFoundException: EmployeeInfo.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at DoPayroll.main(jobexe.java:11)

,我认为在上面的代码中使用 new Scanner(new File("EmployeeInfo.txt") 一旦我输入一个值就会创建新文件。请给我一个简单的解决方案和解释。

I am very new at java and my be missing something very basic. When i run my code i am trying to add value to accounts created in the code. When i try to run the code i recieve an error that a file cannot be found, but i thought that the file was created inside the code.

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class DoPayroll
{
public static void main(String args[])
                                    throws
        IOException     
{
    Scanner diskScanner =
        new Scanner(new File("EmployeeInfo.txt"));

    for (int empNum = 1; empNum <= 3; empNum++)
    {
        payOneEmployee(diskScanner);
    }
}

static void payOneEmployee(Scanner aScanner)
{
    Employee anEmployee = new Employee();

    anEmployee.setName(aScanner.nextLine());
    anEmployee.setJobTitle(aScanner.nextLine());
    anEmployee.cutCheck(aScanner.nextDouble());
    aScanner.nextLine();
}
}

once run i recieve the following error

Exception in thread "main" java.io.FileNotFoundException: EmployeeInfo.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.util.Scanner.<init>(Scanner.java:636)
at DoPayroll.main(jobexe.java:11)

i thought that in the above code using new Scanner(new File("EmployeeInfo.txt") would create the new file once i input a value. Please give me a simple solution and an explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帅气称霸 2024-11-02 22:33:42

当您写入时,它将创建一个新文件。然而,要读取它,它必须已经存在。您可能想检查它是否存在

File file = new File("EmployeeInfo.txt");
if (file.exists()) {
    Scanner diskScanner = new Scanner(file);
    for (int empNum = 1; empNum <= 3; empNum++)
        payOneEmployee(diskScanner);
}

It will create a new file when you write to it. However to read from it, it must already exist. You might like to check it exists with

File file = new File("EmployeeInfo.txt");
if (file.exists()) {
    Scanner diskScanner = new Scanner(file);
    for (int empNum = 1; empNum <= 3; empNum++)
        payOneEmployee(diskScanner);
}
江城子 2024-11-02 22:33:42

File 对象找不到您传递的文件名。您需要将 EmployeeInfo.txt 的完整路径传递给 new File(...) 或确保当前工作目录是包含该文件的目录。

The File object can't find the filename you've passed. You either need to pass the full path of EmployeeInfo.txt to new File(...) or make sure current working directory is the directory that contains this file.

白首有我共你 2024-11-02 22:33:42

File 构造函数不会创建文件。相反,它用 Java 创建访问磁盘上文件所需的信息。您必须使用创建的 File 在 Java 中实际执行文件 IO 才能创建新文件。

扫描器构造函数需要现有的文件。因此,您需要一个指向 EmployeeInfo.txt 的真实有效位置的完整路径,或者首先使用文件 I/O 创建该文件。 本教程有关 Java 中的 I/O 将会有所帮助。

The File constructor does not create a file. Rather, it creates the information in Java needed to access a file on disk. You'd have to actually do file IO in Java using the created File for a new file to be created.

The Scanner constructor requires an existing File. So you need a full path to the real, valid location of EmployeeInfo.txt or to create that file using File I/O first. This tutorial on I/O in Java will help.

浅听莫相离 2024-11-02 22:33:42

您错误地实例化了类 File 的实例,实际上将临时文件写入磁盘。采取这一行

Scanner diskScanner =
        new Scanner(new File("EmployeeInfo.txt"));

并将其替换为以下

File newFile = File.createTempFile("EmployeeInfo", ".txt");
Scanner diskScanner = new Scanner(newFile);

编辑:彼得提出了一个很好的观点。我现在正在捂脸。

You are mistaking instantiating an instance of class File with actually writing a temp file to Disk. Take this line

Scanner diskScanner =
        new Scanner(new File("EmployeeInfo.txt"));

And replace it with this

File newFile = File.createTempFile("EmployeeInfo", ".txt");
Scanner diskScanner = new Scanner(newFile);

Edit: Peter makes a good point. I'm face palming right now.

鲸落 2024-11-02 22:33:42

您认为错误的是:DA Scanner 需要一个现有文件,这似乎很合乎逻辑,因为它读取值,并且如果没有现有文件,则很难读取。 文档还指出:

抛出:
FileNotFoundException - 如果未找到源

因此,简而言之:您必须向扫描仪提供可读的现有文件。

You thought wrong :D A Scanner needs a existing file, which seems quite logical as it reads values and without a existing file its difficult to read. The documentation also states that:

Throws:
FileNotFoundException - if source is not found

So, in short: You must provide a readable, existing file to a scanner.

柠檬 2024-11-02 22:33:42

正如另一个答案所解释的,该文件不仅仅是使用 new File("EmployeeInfo.txt") 创建的。
您可以使用检查文件是否存在

File file = new File("EmployeeInfo.txt");
if(file.exists()) {
  //it exists
}

,或者使用该方法创建文件(如果尚不存在),

file.createNewFile();

如果文件已创建,则返回 true;如果创建,则返回 false它已经存在了。

As the other answer explain, the file is not created just by using new File("EmployeeInfo.txt").
You can check is the file exists using

File file = new File("EmployeeInfo.txt");
if(file.exists()) {
  //it exists
}

or you can create the file (if it doesn't exists yet) using

file.createNewFile();

that method returns true if the file was created and false if it already existed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文