如何以Java8方式在特定URL的文本行上获取流?

如何以Java8方式在特定URL的文本行上获取流?,java,java-8,java-stream,Java,Java 8,Java Stream,我想一行一行地读这些词: 我试图获得一条流: 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

我想一行一行地读这些词:

我试图获得一条流:

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();
Stream=Files.line(path.get(“http://www.puzzlers.org/pub/wordlists/unixdict.txt"));
stream.forEach((word)->System.out.println(word));
//关闭流及其底层文件
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);
}
try(InputStream=newURL(“http://www.puzzlers.org/pub/wordlists/unixdict.txt”).openConnection().getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
Stream=reader.lines()){
stream.forEach(System.out::println);
}
检查此问题:

这个答案似乎最合适

Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\\A").next();

您可以将
.openConnection().getInputStream()
缩短为
.openStream()