Java 找不到文件(File)的合适构造函数

Java 找不到文件(File)的合适构造函数,java,java.util.scanner,Java,Java.util.scanner,我一直在那里错误: error: no suitable constructor found for File(File) File file = new File(testFile); ^ constructor File.File(String,int) is not applicable (actual and formal argument lists differ in length) construc

我一直在那里错误:

    error: no suitable constructor found for File(File)
            File file = new File(testFile);
                        ^
constructor File.File(String,int) is not applicable
  (actual and formal argument lists differ in length)
constructor File.File(String,File) is not applicable
  (actual and formal argument lists differ in length)
constructor File.File(String) is not applicable
  (argument mismatch; File cannot be converted to String)
constructor File.File(String,String) is not applicable
  (actual and formal argument lists differ in length)
constructor File.File(File,String) is not applicable
  (actual and formal argument lists differ in length)
constructor File.File(URI) is not applicable
  (argument mismatch; File cannot be converted to URI)
我不明白错误告诉了我什么,有人能解释一下吗? 这是我的代码:

    public ShortenWord( File testFile ) {
    try {
        File file = new File(testFile);
        Scanner in = new Scanner(file);

        List originalWords = new List();
        List abbWords = new List();

        while (in.hasNextLine())
        {
            String line = in.nextLine();
            String[] parts = line.split(",");
            String originalWord = parts[0];
            String abbWord = parts[1];
        }
    }
catch (FileNotFoundException e)
    {
        System.out.println(e);
    }
}
对于如何修复此错误的任何帮助,我们将不胜感激。因为我一点也不知道
:)

文件
类没有带
文件
参数的构造函数(也称为复制构造函数)

如果您已经将
文件
传递给
ShortenWord
构造函数,只需使用它,而不是尝试创建新的
文件

public ShortenWord( File testFile ) {
  try {
    Scanner in = new Scanner(testFile);

    List originalWords = new List();
    List abbWords = new List();

    while (in.hasNextLine())
    {
        String line = in.nextLine();
        String[] parts = line.split(",");
        String originalWord = parts[0];
        String abbWord = parts[1];
    }
  }
  catch (FileNotFoundException e)
  {
      System.out.println(e);
  }
}

文件构造函数期望文件路径名作为单个参数的字符串。您不应该在文件构造函数中传递另一个文件对象

File file = new File("somefilename.txt");
检查Javadoc:

有4个构造函数,没有一个将文件作为参数:

File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
尝试使用字符串作为参数