您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)獲取RSS源xml文件的方法有哪些,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
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 的內(nèi)容。 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"); // 返回一個(gè) URLConnection 對(duì)象,它表示到 URL 所引用的遠(yuǎn)程對(duì)象的連接。 URLConnection uc = url.openConnection(); // 打開的連接讀取的輸入流。 InputStream in = uc.getInputStream(); int c; while ((c = in.read()) != -1) System.out.print(c); in.close(); } /** * 讀取URL指定的網(wǎng)頁(yè)內(nèi)容 * * @throws IOException */ public static void test2() throws IOException { URL url = new URL("http://rss.mydrivers.com/rss.aspx?Tid=1"); // 打開到此 URL 的連接并返回一個(gè)用于從該連接讀入的 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 的連接并返回一個(gè)用于從該連接讀入的 InputStream。 InputStream in = url.openStream(); int c; while ((c = in.read()) != -1) System.out.print(c); in.close(); } }
關(guān)于獲取RSS源xml文件的方法有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。