来自 URL 文本文件的 Java One-liner Scanner

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6184157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:44:24  来源:igfitidea点击:

Java One-liner Scanner from URL Text file

javaurljava.util.scanner

提问by steve-gregory

In java, what code do I need to get from "http://www.mysite.com/text.txt" to a Scanner that parses the resulting text contained in the site in as few lines as possible.

在 java 中,我需要从“http://www.mysite.com/text.txt”获取哪些代码到扫描器,该扫描器以尽可能少的行解析站点中包含的结果文本。

回答by tanyehzheng

Scanner sc = new Scanner(new URL("http://www.mysite.com/text.txt").openStream());

回答by Jigar Joshi

URL yahoo = new URL("http://www.yahoo.com/");
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yahoo.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();


Reference

参考

回答by Grooveek

Taken from here, not tested

取自此处,未经测试

URLConnection connection = new URL("http://www.mysite.com/text.txt").openConnection();
String text = new Scanner(connection.getInputStream()).useDelimiter("\Z").next();

回答by jmhostalet

an HTTP GETin one line of code: (using Java 8)

一个HTTP GET在一个代码行:(使用Java 8)

String doc = new Scanner(new URL(strUrl).openStream(), "UTF-8").useDelimiter("\A").next();