JAVA 網絡 URL 從網頁上獲取數據 二

package com.mashensoft.net;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
/**
 * 
 * @author PeicongHe
 *
 */
public class GetIpDemo {
    /**
     * 從網頁上獲取數據
     * @param myUrl URL地址
     * @param charset 編碼方式
     * @return 網頁上的數據,String類型
     */
    public static String getContentFromUrl(String myUrl,String charset){
        StringBuffer sb = new StringBuffer();
        URL url;
        try {
            url = new URL(myUrl);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            Scanner sc = new Scanner(is,charset);
            while(sc.hasNextLine()){
                sb.append(sc.nextLine()).append("\r\n");
            }
            sc.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("執行成功");
        return sb.toString();
    }
    /**
     * 功能:保存數據到文件中
     * @param content 要保存的內容
     * @param fileName 目標文件名(路徑)
     * @return boolean 執行是否成功 trun/false
     */
    public static boolean writeContentToFile(String content,String fileName){
        boolean sign = false;
        //把數據存入在文件夾中
        PrintWriter pw;
        try {
            pw = new PrintWriter(fileName);
            pw.println(content);
            pw.flush();
            pw.close();
            sign = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            sign = false;
        }

        return sign;

    }
    /**
     * 從文件中讀取數據
     * @param fileName(文件路徑)
     * @return content
     */
    public static String getContentFromFile (String fileName){
        StringBuffer content = new StringBuffer();
        try {
            Scanner sc = new Scanner(new FileInputStream(fileName));
            while(sc.hasNextLine()){
                content.append(sc.nextLine()).append("\r\n");
            }
            sc.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content.toString();
    }
    /**
     * 閱讀並顯示文件
     * @param fileName 文件名
     */
    public static void readerFile(String fileName){

        char[] myArray = new char[3];
        int len;
        try {
            Reader reader = new InputStreamReader(new FileInputStream(fileName));
            while((len=reader.read(myArray))!=-1){
                System.out.print(new String(myArray));
            }
        }
catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 實現截取字符串中部分字符(例子)
     * int java.lang.String.indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. 
     */
    public static void test(){

        String name = "hepeicong";
        System.out.println(name.indexOf("e"));//獲取字符串中指定某個字符的第一次出現的位置(e)
        System.out.println(name.lastIndexOf("e"));//獲取字符串中指定某個字符的最後一次出現的位置(e)
        int beginIndex = name.indexOf("e");//存放在beginIndex
        int endIndex = name.lastIndexOf("e");//存放在endIndex
        String namex = name.substring(beginIndex+1,endIndex);//截取這兩個字符間的字符
        System.out.println(namex);//輸出
    }
    /**
     * 功能:從文本里獲取IP地址
     * @param content
     * @return ip
     */
    public static String getIpFromContent(String content){
        int beginIndex = content.indexOf("[");
        int endIndex = content.indexOf("]");
        String ip = content.substring(beginIndex,endIndex);//獲取內容
        return ip ;
    }
    /**
     * 功能:從文本內容裏獲取運營商名稱
     * @param content
     * @return ad
     */
    public static String getSpFromContent(String content){
        int beginIndex = content.indexOf("來自:");
        int endIndex = content.indexOf("</center>");
        String ad = content.substring(beginIndex,endIndex);//獲取內容
        return ad;
    } 

    public static void main(String[] args){
        String content = getContentFromUrl("http://1212.ip138.com/ic.asp","gb2312");
        writeContentToFile(content,"c.html");
//      readerFile("c.html");
//      String content = getContentFromFile("c.html");
        System.out.println(getIpFromContent(content));
        System.out.println(getSpFromContent(content));
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章