android doc 本地文档加载慢的解决办法
从来都是FQ上谷歌官网查文档,但是有时没办法FQ,就得用sdk本地的doc文档了,由于文档内部的一些javascript,font等也需要访问Google来加载,导致了打开本地网页也巨慢无比,甚至转了半天圈都打不开,后来经过查找,发现可通过360提供的资源来替换Google的,这样打开的就快得多了。话不多说,上代码批量修改:
package test.qin.fileio; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { <span style="white-space:pre"> </span>ExecutorService executors = Executors.newFixedThreadPool(2); <span style="white-space:pre"> </span>public static void main(String[] args) throws InterruptedException { <span style="white-space:pre"> </span>Main m = new Main(); <span style="white-space:pre"> </span>File f = new File("F:\\sdk\\docs"); <span style="white-space:pre"> </span>m.googleCDNChange(f); <span style="white-space:pre"> </span>m.close(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * 将谷歌CDN修改为360CDN <span style="white-space:pre"> </span> * <span style="white-space:pre"> </span> * @param rootFile <span style="white-space:pre"> </span> * 本地文件根目录,或本地文件 <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public void googleCDNChange(File rootFile) { <span style="white-space:pre"> </span>if (rootFile != null && rootFile.exists()) { <span style="white-space:pre"> </span>// 是个文件夹,则修改它包含的文件 <span style="white-space:pre"> </span>if (rootFile.isDirectory()) { <span style="white-space:pre"> </span>File[] childs = rootFile.listFiles(); <span style="white-space:pre"> </span>for (File f : childs) { <span style="white-space:pre"> </span>googleCDNChange(f); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} else { <span style="white-space:pre"> </span>// 是个文件,则修改自身 <span style="white-space:pre"> </span>ReplaceRunnable r = ReplaceRunnable.CreateNew(rootFile, this); <span style="white-space:pre"> </span>star(r); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * 替换字符串 <span style="white-space:pre"> </span> * <span style="white-space:pre"> </span> * @param f <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public void replaceString(File f) { <span style="white-space:pre"> </span>try { <span style="white-space:pre"> </span>String fileName = f.getName(); <span style="white-space:pre"> </span>if (!fileName.endsWith(".html") && !fileName.endsWith(".js")) { <span style="white-space:pre"> </span>return; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println(fileName + "开始修改"); <span style="white-space:pre"> </span>FileReader fileInputStream = new FileReader(f); <span style="white-space:pre"> </span>BufferedReader bufferedReader = new BufferedReader(fileInputStream); <span style="white-space:pre"> </span>StringBuffer sb = new StringBuffer(); <span style="white-space:pre"> </span>String tmp = null; <span style="white-space:pre"> </span>while ((tmp = bufferedReader.readLine()) != null) { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>tmp = tmp.replace("fonts.googleapis.com", "fonts.useso.com"); <span style="white-space:pre"> </span>tmp = tmp.replace("ajax.googleapis.com", "ajax.useso.com"); <span style="white-space:pre"> </span>tmp = tmp.replace("www.google.com/jsapi", <span style="white-space:pre"> </span>"ajax.useso.com/jsapi"); <span style="white-space:pre"> </span>tmp = tmp.replace("www.google-analytics.com", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("www.google-analytics.com/analytics.js", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("ga('create', 'UA-5831155-1', 'android.com');", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("ga('create', 'UA-49880327-2', 'android.com', {'name': 'universal'});", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("ga('send', 'pageview');", ""); <span style="white-space:pre"> </span>tmp = tmp.replace("ga('universal.send', 'pageview');", ""); <span style="white-space:pre"> </span>sb.append(tmp); <span style="white-space:pre"> </span>sb.append(System.getProperty("line.separator")); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>bufferedReader.close(); <span style="white-space:pre"> </span>FileWriter fileWriter = new FileWriter(f); <span style="white-space:pre"> </span>fileWriter.write(sb.toString().replace("","")); <span style="white-space:pre"> </span>fileWriter.flush(); <span style="white-space:pre"> </span>fileWriter.close(); <span style="white-space:pre"> </span>System.out.println(fileName + "修改完毕"); <span style="white-space:pre"> </span>} catch (FileNotFoundException e) { <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>} catch (IOException e) { <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void star(Runnable r) { <span style="white-space:pre"> </span>executors.execute(r); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void close() { <span style="white-space:pre"> </span>executors.shutdown(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>static class ReplaceRunnable implements Runnable { <span style="white-space:pre"> </span>public static ReplaceRunnable CreateNew(File f, Main m) { <span style="white-space:pre"> </span>ReplaceRunnable r = new ReplaceRunnable(); <span style="white-space:pre"> </span>r.f = f; <span style="white-space:pre"> </span>r.m = m; <span style="white-space:pre"> </span>return r; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>private File f; <span style="white-space:pre"> </span>private Main m; <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>m.replaceString(f); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> }
版权声明:本文为博主原创文章,未经博主允许不得转载。