java - 文件类错误 -

标签 java file class

我正在遵循教程,到目前为止示例代码正在运行。这是关于 I/O 的,提供的代码会抛出错误消息:

EchoSquareDisk.java:8: error: constructor File in class File cannot be applied to given types;

File    file = new File("myData.txt");   // create a File object  
               ^

required: no arguments
found: String
reason: actual and formal argument lists differ in length
EchoSquareDisk.java:9: error: no suitable constructor found for Scanner(File)

Scanner scan = new Scanner( file );      // connect a Scanner to the file

这是从教程复制的代码:

import java.util.Scanner;
import java.io.*;

class EchoSquareDisk {
    public static void main (String[] args) throws IOException { 
        File    file = new File("myData.txt");   // create a File object
        Scanner scan = new Scanner( file );      // connect a Scanner to the file
        int num, square;

        num = scan.nextInt();
        square = num * num ;   

        System.out.println("The square of " + num + " is " + square);
    }
}

我创建了一个 myData.txt 文件,并按照要求将字符 9000 放入其中。

最佳答案

好吧,作为一个初学者,我忽略了教程另一部分中位于同一目录中的已有 File.class 文件。

我已将 File.java 重命名为 old_File.java,因为我怀疑这是错误的原因,但我不介意 File.class。一旦我删除了 File.class,一切就正常了。

关于java - 文件类错误 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50314011/

相关文章:

c++ - fstream <无法读取内存>。无法打印列表中的所有项目

Linux,无主目录设置上的命令行程序配置文件

java - 使用 Jersey 通过 RESTfull 服务上传文件和资源配置不可修改?

java - 努力在我的 Android Studio 项目中获取 Android View

java - 尽管权限在 list 中,但需要 VIBRATE 权限异常

Java:从大文件中获取随机行

objective-c - Objective C 按类创建对象

c++ - C++中的多个降序对象

java接口(interface)和新类

java - getTitle() 在 Selenium 中未显示正确的输出