代码有什么问题? Horstmann 书中的第一个代码示例

What is wrong with the code? First code example from Horstmann book

我从 Horstmann 书(第 2 卷)中复制了代码示例,但不明白为什么它不起作用。你能帮助我吗?我试图删除 IOException,但它引发了另一个问题

package streams;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class Hello {

public static void main(String[] args) throws IOException
{
    String contents = new String(Files.readAllBytes(Paths.get("text.txt")), StandardCharsets.UTF_8);
    List<String> words = Arrays.asList(contents.split("\PL+"));

    long count = 0;
    for(String w : words)
    {
        if (w.length() > 12) count++;
    }
    System.out.println(count);

    count = words.stream().filter(w -> w.length() > 12).count();
    System.out.println(count);

    count = words.parallelStream().filter(w -> w.length() > 12).count();
    System.out.println(count);
}

}

Console log

您在创建新的 java class 时是否创建了一个名为 Hello 的 class?错误正在清除,说明它找不到您的 class,因此抛出错误。尝试使用默认包重新创建另一个 java 项目,然后在该默认包中创建一个新的 hello class。 运行 一个简单的 println 并查看它是否有效,然后尝试将旧代码复制到该新文件中。希望这有帮助:)