尝试使用Dr.Java打开文件时编译错误

时间:2017-04-11 15:23:15

标签: java

所以我目前正在大学读一门java课程,到目前为止,我已经成功地找到并解决了这些问题。到现在。当我尝试打开并读取文件时,我遇到了某种编译错误。我正在关注我的课本教科书,其基本代码的设置说明是这样做的,但它以错误结束。然后,我从我的书中获取了源代码并将其放入Dr.Java中,但它仍然编译错误,而且来自本书。所以我不确定问题是什么,并且想知道是否有人可以引导我朝着正确的方向前进。 F.Y.I.它总是相同的两个错误。谢谢。

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)

1 个答案:

答案 0 :(得分:1)

您似乎有一些不是File的自定义java.io.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