我正在遵循教程,到目前为止示例代码正在运行。这是关于 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/