如今买票是一大难事,在高峰时段 打开12306网站,慢的像蜗牛,想到以前用修改hosts文件来登录Google(Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统再会将网址提交DNS域名解析服务器进行IP地址的解析。),那么我也来试试这种方法来登录下12306,用搜索找到几个解析IP的网站,试了下,果然奏效(现在).为了方便使用,我把IP写到本地文件.
下面是JAVA的相关代码,显示把IP写到D盘的ip.txt中.
package Demo; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; public class testREG_IP { // 指定文件路径和名称 private static String path = "D:/ip.txt"; private static File fileName = new File(path); private static String readStr = ""; private static BufferedReader bufferReader; public static void main(String[] args) throws IOException { //网址 String path = "http://www.17ce.com/site/http/201401_17ccef99e3d1ca9c19aea3c3d19136dc.html"; //正则表达式,过滤IP格式 String regex = "\d+\.\d+\.\d+\.\d+"; String ipText = getWeb(path, regex); createTxtFile(); readTxtFile(); System.out.println(ipText); writeTxtFile(ipText); } /** * 创建文件 * * @throws IOException */ private static void createTxtFile() throws IOException { if (!fileName.exists()) { fileName.createNewFile(); System.err.println(fileName + " 已创建!"); } } /** * 读文件 */ public static String readTxtFile() { String read; FileReader fileread; try { fileread = new FileReader(fileName); bufferReader = new BufferedReader(fileread); try { while ((read = bufferReader.readLine()) != null) { readStr = readStr + read + " "; } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return readStr; } /** * 写文件 */ public static void writeTxtFile(String newStr) throws IOException { // 追加内容 String filein = newStr + " " + readStr + " "; RandomAccessFile mm = null; try { mm = new RandomAccessFile(fileName, "rw"); mm.writeBytes(filein); System.out.println("写入成功"); } catch (IOException e1) { System.out.println("写入失败"); e1.printStackTrace(); } finally { if (mm != null) { try { mm.close(); } catch (IOException e2) { System.out.println("写入失败"); e2.printStackTrace(); } } } } //获取ip public static String getWeb(String ip, String regex) { URL url; final StringBuffer stringBuffer = new StringBuffer(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = formatter.format(new Date()); stringBuffer.append(" ----------" + time + "----------- "); try { url = new URL(ip); HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.connect(); InputStream in = urlConnection.getInputStream(); byte[] data = new byte[1024]; while (in.read(data) > 0) { // 读取流文件 String tempString = new String(data); Matcher m = Pattern.compile(regex).matcher(tempString); while (m.find()) { stringBuffer.append(m.group()); stringBuffer.append(" "); } } } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } }