我想逐行阅读这些词:http://www.puzzlers.org/pub/wordlists/unixdict.txt
我尝试获取流:
Stream<String> stream = Files.lines(Paths.get("http://www.puzzlers.org/pub/wordlists/unixdict.txt"));
stream.forEach((word) -> System.out.println(word));
//Close the stream and it's underlying file as well
stream.close();
但我怀疑它只适用于文件。是否有类似的 URL 方法?
最佳答案
BufferedReader
还有一个返回流的 lines()
方法。所以你只需要打开一个 BufferedReader 包装一个 InputStreamReader 包装 URL 连接输入流:
try (InputStream is = new URL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Stream<String> stream = reader.lines()) {
stream.forEach(System.out::println);
}
关于java - 如何以 Java 8 方式获取特定 URL 上的文本行流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33691049/