我的java代码有缺陷,但我不明白为什么
我对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您写入时,它将创建一个新文件。然而,要读取它,它必须已经存在。您可能想检查它是否存在
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
对象找不到您传递的文件名。您需要将EmployeeInfo.txt
的完整路径传递给new File(...)
或确保当前工作目录是包含该文件的目录。The
File
object can't find the filename you've passed. You either need to pass the full path ofEmployeeInfo.txt
tonew File(...)
or make sure current working directory is the directory that contains this file.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 createdFile
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 ofEmployeeInfo.txt
or to create that file using File I/O first. This tutorial on I/O in Java will help.您错误地实例化了类 File 的实例,实际上将临时文件写入磁盘。采取这一行
并将其替换为以下
编辑:彼得提出了一个很好的观点。我现在正在捂脸。
You are mistaking instantiating an instance of class File with actually writing a temp file to Disk. Take this line
And replace it with this
Edit: Peter makes a good point. I'm face palming right now.
您认为错误的是:DA
Scanner
需要一个现有文件,这似乎很合乎逻辑,因为它读取值,并且如果没有现有文件,则很难读取。 文档还指出:因此,简而言之:您必须向扫描仪提供可读的现有文件。
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:So, in short: You must provide a readable, existing file to a scanner.
正如另一个答案所解释的,该文件不仅仅是使用
new File("EmployeeInfo.txt")
创建的。您可以使用检查文件是否存在
,或者使用该方法创建文件(如果尚不存在),
如果文件已创建,则返回
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
or you can create the file (if it doesn't exists yet) using
that method returns
true
if the file was created andfalse
if it already existed.