!!! Surprise at the bottom!!!
1. Update Hosts
Import Java.awt.desktop;import java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.File;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.filewriter;import java.io.IOException; Import Java.io.inputstreamreader;import Java.net.malformedurlexception;import Java.net.uri;import Java.net.urisyntaxexception;import java.net.url;/** * Update the Hosts file * * @author Mrchu * @version 1.0 * @date January 16, 2015 * @se E www.chuweibiao.com */public class Updatehosts {/** HTML page path */private static final String Page_url = "HTTP://WWW.360KB.C Om/kb/2_122.html ";/** HOSTS file path */private static final String Hosts_path =" c:\\windows\\system32\\drivers\\etc\\hosts ";/** HOSTS backup file path */private static final String Hosts_bak_path =" C:\\windows\\system32\\drivers\\etc\\hosts.bak ";/** * Get HTML content based on page URL * @param pageurl * Page URL * @return * HTML content */public static string gethtmlcontent (String pageurl) {STRINGB Uffer sb = new StringBuffer (); BufferedReader in = null;try {URL url = new URL (Pageurl); in = new BufferedReader (New InputStreamReader (Url.openstream (), "Utf-8")); String temp;while (temp = In.readline ())! = null) {sb.append (temp);}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {try { In.close ();} catch (IOException e) {e.printstacktrace ();}} int begin = Sb.indexof ("#google hosts by 360kb.com"), int end = Sb.indexof ("#google hosts") + 22;return sb.su Bstring (begin, end). toString (); }/** * Convert HTML content * @param pageurl * Page URL * @return * converted HTML content */public static string converthtmlcontent (String pageurl) {String content = gethtmlcontent (pageurl); content = Content.replaceall ("(<br/>) +?", "\ r \ n"); content = Content.replaceall (" ", ""); content = Content.replaceall (" ", ""); content = Content.replaceall ("", "" Content = Content.replaceall ("</span></p><p><span><span style=\" line-height:22px;\ " ><span> "," \ r \ n "); content = Content.replacEall ("<span> </span>", ""); content = Content.replaceall ("</span>", ""); content = Content.replaceall ("</span>", ""); return content;} /** * Read Hosts file * @param filePath * Hosts file path * @return * Hosts file contents */public static string readhosts (String filePath) {Fil EReader filereader = null; BufferedReader bufferedreader = null; StringBuilder sb = new StringBuilder (); try {filereader = new FileReader (filePath); BufferedReader = new BufferedReader (fil EReader); String line = null;while (line = Bufferedreader.readline ())! = null) {Sb.append (line). Append ("\ r \ n");} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {try { Filereader.close (); Bufferedreader.close ();} catch (IOException e) {e.printstacktrace ();}} return sb.tostring ();} /** * Update Hosts file * @param filePath * Hosts file path * @param content * Hosts file contents */public static void Updatehosts (String filepat H, String content) {FileWriter FileWriter = null; BufferedWriter Bufferedwriter = null;try {File File = new file (FilePath), if (!file.exists ()) {file.createnewfile ();} FileWriter = new FileWriter (filePath); bufferedwriter = new BufferedWriter (fileWriter); bufferedwriter.write (content);} catch (IOException e) {e.printstacktrace ();} finally {try {filewriter.flush (); Bufferedwriter.flush (); Filewriter.close (); Bufferedwriter.close ();} catch (IOException e) {e.printstacktrace ();}}} public static void Main (string[] args) {//Hosts file backup String hostscontent = ""; File File = new file (Hosts_bak_path), if (File.exists ()) {hostscontent = Readhosts (Hosts_bak_path);} else {hostscontent = R Eadhosts (Hosts_path); Updatehosts (Hosts_bak_path, hostscontent);} The HOSTS file is updated with string newhostscontent = Hostscontent + "\ r \ n" + converthtmlcontent (Page_url); Updatehosts (Hosts_path, Newhostscontent); System.out.println ("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽"); System.out.println ("\r\n▇▆▄▃▂▁hosts file updated successfully! ▁▂▃▄▆▇\r\n "); System.out.println ("");//browser Access test feasibility try {uri uri = new Uri ("Http://www.google.com/"); Desktop.getdesktop (). browse (URI); } catch (URISyntaxException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }}}
2. Restore Hosts
Import java.io.file;/** * Restore Hosts file * * @author Mrchu * @version 1.0 * @date January 16, 2015 * @see www.chuweibiao.com */PUBLI C class Restorehosts {/** HOSTS file path */private static final String Hosts_path = "C:\\windows\\system32\\drivers\\etc\\host S ";/** HOSTS backup file path */private static final String Hosts_bak_path =" C:\\windows\\system32\\drivers\\etc\\hosts.bak ";/** * Restore HOSTS File * @return * true: Restore succeeded, false: Restore failed */public static Boolean restorehosts () {File File = new file (Hosts_bak_path); if (!file.exists ()) {return false;} else {updatehosts.updatehosts (Hosts_path, Updatehosts.readhosts (Hosts_bak_path)) ; return File.delete ();}} public static void Main (string[] args) {if (restorehosts ()) {System.out.println ("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽"); System.out.println ("\r\n▇▆▄▃▂▁hosts file restore succeeded! ▁▂▃▄▆▇\r\n "); System.out.println ("");} else {System.out.println ("\r\n▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽▽"); SYSTEM.OUT.PRINTLN ("\r\n▇▆▄▃▂▁ Restore failed, hosts backup file does not exist!") ▁▂▃▄▆▇\r\n "); System.out.println ("");}}}
Crossing The Great Wall ( the most simple and pollution-free turning wall in history ... ):
http://download.csdn.net/detail/for_china2012/8368737
Windows Hosts for Google, the hole!