首页 > 编程 > Java > 正文

JAVA 网络 URL 从网页上获取数据 二

2019-11-09 19:42:22
字体:
来源:转载
供稿:网友
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)); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表