简体   繁体   English

使用Scanner类从文件读取信息时遇到问题

[英]Having trouble reading information from a file using Scanner class

I wrote this simple program to try to read information from a txt file in my computer's D drive` 我写了这个简单的程序,试图从计算机D驱动器中的txt文件中读取信息。

package readDisk;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;

public class ReadDisk 
{
    public static void main(String[] args) {
    Scanner input = new Scanner(Path.of("D:\\test.txt"), StandardCharsets.UTF_8);
    String TestText = input.nextLine();
    System.out.println(TestText);
    }
}

I am getting an error message upon compilation which goes 编译时出现错误消息

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method of(String) is undefined for the type Path

    at readDisk.ReadDisk.main(ReadDisk.java:9)

I am following a sample program found in the 11th Edition of Core Java Volume 1 and I've looked all over, trying to find where I've gone wrong to no avail. 我正在跟踪在Core Java Volume 1的第11版中找到的示例程序,而且我四处张望,试图找出哪里出了错而无济于事。 Any help will be appreciated. 任何帮助将不胜感激。

As opposed to what some commenters say, the method you're trying to use does actually exist . 与某些评论者所说的相反,您尝试使用的方法确实存在 The method in question takes a required first argument, and then a variable number of arguments, effectuated by the varargs construct , which means zero or more arguments. 所讨论的方法需要一个必需的第一个参数,然后是可变数量的参数,这些参数varargs构造实现,这意味着零个或多个参数。

But it is only available from Java 11 onwards. 但是它仅可从Java 11开始使用。 You need to check your Java version. 您需要检查您的Java版本。

An alternative would be that you use scanner with another argument: 一种替代方法是您将扫描器与另一个参数一起使用:

  • new Scanner(new File(D:/test.txt), StandardCharsets.UTF_8) ; new Scanner(new File(D:/test.txt), StandardCharsets.UTF_8) ; or 要么
  • new Scanner(Paths.get(D:/test.txt), StandardCharsets.UTF_8)

The constructors throw a FileNotFoundException and an IOException respectively. 构造函数分别抛出FileNotFoundExceptionIOException Make sure you either handle it or propagate it to the caller. 确保您处理它或将其传播给呼叫者。


Note: A quick local test has shown me that this actually works for me. 注意:快速的本地测试向我表明这实际上对我有用。 So if your code still throws a FileNoteFoundException , my guess is there is something otherwise wrong with the file or filename. 因此,如果您的代码仍然抛出FileNoteFoundException ,我猜是文件或文件名存在其他问题。

尝试按以下方式初始化扫描仪,您无需为此设置Path:

Scanner input = new Scanner(new File("D:\\test.txt") , StandardCharsets.UTF_8);

The Path.of() method, added in JDK 11, requires a URI as parameter, not a String . JDK 11中添加的Path.of()方法需要URI作为参数,而不是String For example, 例如,

Scanner input = new Scanner(Path.of(new URI("file:///D:/test.txt")), StandardCharsets.UTF_8);

Or you can simply use new Scanner(File) , as other answer said. 或者您可以简单地使用new Scanner(File) ,如其他答案所述。

Your code is fine Path::of method can take only one argument since the second one is vararg. 您的代码很好, Path::of方法只能接受一个参数,因为第二个参数是vararg。 Just make sure you are using java 11 只要确保您使用的是Java 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM