Android development help document Doc open speed slow solution _ Python,

Source: Internet
Author: User

Android development help document Doc open speed slow solution _ Python,

Slow opening of android help documentation

The reason is that the html file under the Doc directory contains js files for accessing google.

<link rel="stylesheet"href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">
And

 <script src="http://www.google.com/jsapi" type="text/javascript"></script>
It is found that.

Because these two scripts need to access Google online, it is clear that the subsequent content loading will take a long time ......

What should we do?

Can I open the. html file under each directory and manually delete the content of the above two rows? It will definitely be deleted. Someone suggested:

Method 1: Modify the Hosts file

Modify the hosts file in the C: \ WINDOWS \ system32 \ drivers \ etc directory to add

127.0.0.1 fonts.googleapis.com
127.0.0.1www.google.com
127.0.0.1www.google.com/jsapi
127.0.0.1www.google-analytics.com
127.0.0.1 apis.google.com/js/

The speed will increase a lot.

Method 2: compile Java program batch comments

Traverse all files under the doc directory and delete the content of the above two lines of each file. For more information, see

/** Remove the javascript code that requires Internet connection in the Android document */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; public class FormatDoc {public static int j = 1;/*** @ param args */public static void main (String [] args) {File file = new File ("D:/android-sdk-windows/docs/ "); SearchDirectory (file, 0); System. out. println (" OVER ");} public static void searchDirectory (File f, int depth) {if (! F. isDirectory () {String fileName = f. getName (); if (fileName. matches (". *. {1} html ") {String src =" <(link rel) [=] \ "(stylesheet) \" \ n (href) [=] \ "(http): // (fonts.googleapis.com/css) [?] (Family) [=] (Roboto) [:] (regular, medium, thin, italic, mediumitalic, bold) \ "(title) [=] \ "roboto \"> "; String src1 =" <script src = \ "http://www.google.com/jsapi\" type = \ "text/javascript \"> </script> "; string dst = ""; // if it is an html file, comment out the specific javascript code annotation (f, src, dst); annotation (f, src1, dst );}} else {File [] fs = f. listFiles (); depth ++; for (int I = 0; I <fs. length; ++ I) {File file = fs [I]; searc HDirectory (file, depth );}}} /** f File in which specific content will be modified * src content to be replaced * dst content to be replaced */public static void annotation (File f, String src, string dst) {String content = FormatDoc. read (f); content = content. replaceFirst (src, dst); int ll = content. lastIndexOf (src); System. out. println (ll); FormatDoc. write (content, f); System. out. println (j ++); return;} public static String read (File src) {StringBuffer re S = new StringBuffer (); String line = null; try {BufferedReader reader = new BufferedReader (new FileReader (src); int I = 0; while (line = reader. readLine ())! = Null) {if (I! = 0) {res. append ('\ n');} res. append (line); I ++;} reader. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return res. toString ();} public static boolean write (String cont, File dist) {try {BufferedWriter writer = new BufferedWriter (new FileWriter (dist); writer. write (cont); writer. flush (); writer. close (); return true;} catch (IOException e) {e. printStackTrace (); return false ;}}}

Method 3: Execute the script

Deleting the line of js Code through shell is very simple and convenient, which is 100 times more convenient than the preceding java code, but cannot delete the first js Code.

find . -name "*.html"|xargs grep -l "jsapi"|xargs sed -i '/jsapi/d'
I have never tried.


My life is short. I use Python

Method 4: Batch Delete python code

Train of Thought: search for all .html files, open these files, read the file content, replace the above js content with null, write the modification content back to the file, and end.

It's complicated to say. It's really easy to implement it using python.

import oss1 = '''<link rel="stylesheet"href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''s2 = '''<script src="http://www.google.com/jsapi" type="text/javascript"></script>'''s3 = '''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>'''s4 = '''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>'''for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):    for file in files:        fd = root + os.sep + file        if ".html" in fd:            print fd            f = open(fd, 'r')            s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")            f.close()            f = open(fd, 'w')            f.write(s)            f.close()      

Assume that my Android development help document is under c: \ androidsdk \ docs, and you only need to use the OS walk function to traverse all directories and file names.

for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):
The Return Value of the walk. The root directory to be traversed, The subdirectories dirs under it, and the files to be traversed. The important thing is to recursively traverse the specified directory C: \ AndroidSdk \ docs.

Remove the two js files that affect the speed in the html file. You must first have an html file,

    for file in files:        fd = root + os.sep + file
All file names are created, and you only need to find the (matching character .html File

        if ".html" in fd:            print fd
The next step is to get rid of the js of the shadow display (speed). Replace the corresponding content in the file with null.

f = open(fd, 'r')            s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")            f.close()            f = open(fd, 'w')            f.write(s)            f.close()

Run it. It will end in 1 minute. There are more than nine thousand files in the whole docs, and it takes time to traverse and read/write.

Finally, find a doc and try C: \ AndroidSdk \ docs \ reference \ android \ widget \ Spinner.html.

That's the speed.

I use python2.7.5.

Thank you! Or click like!


Python https://www.python.org/ftp/python/2.7.9/python-2.7.9.msi





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.