[英]Compile error while trying to open a file using Dr.Java
因此,我目前正在大學里學習Java課程,到目前為止,我已經成功地解決了問題並解決了問題。 到現在。 嘗試打開和讀取文件時出現某種編譯錯誤。 我按照班級教科書的基本說明進行設置,但這樣做以錯誤告終。 然后,我從書中獲取源代碼,並將其放入Dr.Java中,但仍然編譯錯誤,而那是書中的內容。 因此,我不確定問題出在哪里,並且想知道是否有人可以引導我朝正確的方向前進。 僅供參考,它總是兩個相同的錯誤。 謝謝。
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for the File class
/**
This program reads data from a file.
*/
public class FileReadDemo
{
public static void main(String[] args) throws IOException
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the filename.
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
// Open the file.
File file = new File(filename);
Scanner inputFile = new Scanner(file);
// Read lines from the file until no more are left.
while (inputFile.hasNext())
{
// Read the next name.
String friendName = inputFile.nextLine();
// Display the last name read.
System.out.println(friendName);
}
// Close the file.
inputFile.close();
}
}
這是不斷發生的錯誤代碼。
找到2個錯誤:
File: C:\Users\aspea\Documents\Intro to Computers and Java\FileReadDemo.java [line: 20]
Error: constructor File in class File cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length
File: C:\Users\aspea\Documents\Intro to Computers and Java\FileReadDemo.java
[line: 21]
Error: no suitable constructor found for Scanner(File)
constructor java.util.Scanner.Scanner(java.lang.Readable) is not applicable
(argument mismatch; File cannot be converted to java.lang.Readable)
constructor java.util.Scanner.Scanner(java.io.InputStream) is not applicable
(argument mismatch; File cannot be converted to java.io.InputStream)
constructor java.util.Scanner.Scanner(java.io.File) is not applicable
(argument mismatch; File cannot be converted to java.io.File)
constructor java.util.Scanner.Scanner(java.nio.file.Path) is not applicable
(argument mismatch; File cannot be converted to java.nio.file.Path)
constructor java.util.Scanner.Scanner(java.lang.String) is not applicable
(argument mismatch; File cannot be converted to java.lang.String)
constructor java.util.Scanner.Scanner(java.nio.channels.ReadableByteChannel) is not applicable
(argument mismatch; File cannot be converted to java.nio.channels.ReadableByteChannel)
看來您有一些不是java.io.File
自定義File
類,以下錯誤表明了這一事實:
Error: constructor File in class File cannot be applied to given types;
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length
和
File cannot be converted to java.io.File
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.