Не найдено подходящего конструктора для файла (файла)

1

Я получаю там ошибку:

    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);
    }
}

Любая помощь в том, как исправить эту ошибку, будет с благодарностью. Поскольку у меня нет подсказки :)

Теги:
java.util.scanner

3 ответа

3
Лучший ответ

Конструктор файлов ожидает имя файла как String для одного параметра. Вы не должны передавать другой файл в конструктор файла.

File file = new File("somefilename.txt");
2

Класс File не имеет конструктора с аргументом File (также называемым конструктором копирования).

Если вы уже передаете File в конструктор ShortenWord, просто используйте его вместо того, чтобы пытаться создать новый File:

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);
  }
}
1

Проверьте Javadoc: http://docs.oracle.com/javase/7/docs/api/java/io/File.html

Существует 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.

Попробуйте использовать параметр String as.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню