[英]Java Text Input: How to ignore lines starting with certain characters in them?
[英]How to get stream over lines of text at certain URL in Java 8 way?
我想逐行阅读这些文字: 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()
方法。 所以你只需要打开一个包装了一个包含URL连接输入流的InputStreamReader的BufferedReader:
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代码中读取url to string
这个答案似乎是最合适的
Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\\A").next();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.