LoginSignup
6
6

More than 5 years have passed since last update.

URLから読み込んだ内容をStream APIで処理

Posted at

たとえばhttp://www.yahoo.co.jpとかの文字列からHTMLを読み込んで来て、Stream APIで処理をしたい場合。

  1. URLからInputStream
  2. InputStreamからBufferedReader
  3. BufferedReader#linesでStreamを取得

以下のコードはforEachしてるだけだけど、お好みで適当な処理を追加すればおk

    public static void main(String[] args) throws MalformedURLException, IOException {
        URL url = new URL("http://www.yahoo.co.jp");
        try (
            InputStream in = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));) {
            reader.lines().forEach(System.out::println);
        }
    }
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6