// This example uses the text of Sina news as an example.
@ Testpublic boolean getContent (String link) throws Exception {// get the corresponding input stream URL = new url (link) through the link; URLConnection urlConnection = URL. openConnection (); InputStream inputStream = urlConnection. getInputStream (); // Save the stream to a byte [] array contentByte byte [] contentByte = new byte [1024*10]; byte [] buffer = new byte [1024]; int I =-1; while (I = inputStream. read (buffer ))! =-1) {contentByte = byteMerger (contentByte, buffer);} // convert the array into a string, and then add, delete, modify, and query the string. note the encoding when converting byte [] arrays into strings. It must be consistent with the link source encoding String str = new String (contentByte, "gb2312 "); // The following is the truncation of the string int start = str. indexOf ("
"); Int end = str. indexOf ("
"); If (start! =-1) {// if the search is successful, that is, the source file has the target to be intercepted. // The String contentStr = str to be truncated. substring (start, end); // Add necessary html code. The main conversion formats must also be one-to-one. the added html encoding must be consistent with byte [] write1 ="
". GetBytes (" GB2312 "); byte [] write = contentStr. getBytes (" GB2312 "); byte [] write2 ="". GetBytes (); OutputStream outputStream = new FileOutputStream (Environment. getExternalStorageDirectory (). toString () + "/tmp.html"); // use the byteMerger method to connect to the byte [] array outputStream. write (byteMerger (write1, write), write2); outputStream. close (); return true;} else {// if it fails, save the source file. check the cause of the Error. FileWriter fw = new FileWriter (Environment. getExternalStorageDirectory (). toString () + "/aa.txt"); fw. flush (); fw. write (str); fw. close (); return false ;}}// java merges two byte Arrays: public static byte [] byteMerger (byte [] byte_1, byte [] byte_2) {byte [] byte_3 = new byte [byte_1.length + byte_2.length]; System. arraycopy (byte_1, 0, byte_3, 0, byte_1.length); System. arraycopy (byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3 ;}