温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

获取RSS源xml文件的方法有哪些

发布时间:2020-10-29 10:37:25 来源:亿速云 阅读:148 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关获取RSS源xml文件的方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

package com.cyz;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

public class TestURL {

    public static void main(String[] args) throws IOException {
//        test4();
//        test3();
        test2();
//        test();
    }

    /**
     * 获取URL指定的资源。
     * 
     * @throws IOException
     */
    public static void test4() throws IOException {
        URL url = new URL("http://rss.mydrivers.com/rss.aspx?Tid=1");
        // 获得此 URL 的内容。
        Object obj = url.getContent();
        System.out.println(obj.getClass().getName());
    }

    /**
     * 获取URL指定的资源
     * 
     * @throws IOException
     */
    public static void test3() throws IOException {
        URL url = new URL("http://rss.mydrivers.com/rss.aspx?Tid=1");
        // 返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
        URLConnection uc = url.openConnection();
        // 打开的连接读取的输入流。
        InputStream in = uc.getInputStream();
        int c;
        while ((c = in.read()) != -1)
            System.out.print(c);
        in.close();
    }

    /**
     * 读取URL指定的网页内容
     * 
     * @throws IOException
     */
    public static void test2() throws IOException {
        URL url = new URL("http://rss.mydrivers.com/rss.aspx?Tid=1");
        // 打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
        Reader reader = new InputStreamReader(new BufferedInputStream(url.openStream()), "UTF-8");
        int c;
        while ((c = reader.read()) != -1) {
            System.out.print((char) c);
        }
        reader.close();
    }

    /**
     * 获取URL的输入流,并输出
     * 
     * @throws IOException
     */
    public static void test() throws IOException {
        URL url = new URL("http://rss.mydrivers.com/rss.aspx?Tid=1");
        // 打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
        InputStream in = url.openStream();
        int c;
        while ((c = in.read()) != -1)
            System.out.print(c);
        in.close();
    }
}

关于获取RSS源xml文件的方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI