写一个java程序访问url,并根据stream算出响应时间及读取打印页面内容

本文档介绍如何编写一个Java程序,利用Stream API来计算访问URL的响应时间,并读取并打印页面内容。程序关注于DOCTYPE html部分的输出结果。
摘要由CSDN通过智能技术生成

写一个java程序访问url,并根据stream算出响应时间及读取打印页面内容

package cn.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class duqubaidu {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.baidu.com");
        long start = System.currentTimeMillis();
        InputStream openStream = url.openStream();
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(openStream));

        String tmp = null;
        while((tmp = reader.readLine()) != null) {
            builder.append(tmp + "\n");
           
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start) + "ms");
         System.out.println(builder.toString());

    }

    }

输出结果 :主要看 DOCTYPE html

"D:\Program Files\Java\jdk1.8.0_141\bin\java" "-javaagent:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=59519:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_141\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\rt.jar;F:\spark\target\classes;E:\develop\repository_pinyougou\org\scala-lang\scala-library\2.11.8\scala-library-2.11.8.jar;E:\develop\repository_pinyougou\org\apache\spark\spark-core_2.11\2.1.3\spark-core_2.11-2.1.3.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-mapred\1.7.7\avro-mapred-1.7.7-hadoop2.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7.jar;E:\develop\repository_pinyougou\org\apache\avro\avro\1.7.7\avro-1.7.7.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7-tests.jar;E:\develop\repository_pinyougou\org\codehaus\jackson\jackson-core-asl\1.9.13\jackson-core-asl-1.9.13.jar;E:\develop\repository_pinyougou\org\codehaus\jackson\jackson-mapper-asl\1.9.13\jackson-mapper-asl-1.9.13.jar;E:\develop\repository_pinyougou\com\twitter\chill_2.11\0.8.0\chill_2.11-0.8.0.ja
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值