import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.UUID; /** * @author hutuanle * doc文档太慢? 把这个类复制为java文件,编译运行即可; * 只要修改doc_path为你使用的sdk下的doc路径即可 * 比如我的doc路径为doc_path = "D:\\android_tools\\android-sdk_r20.0.3-windows\\android-sdk-windows\\docs" * 我是修改下了别人大牛的代码 去掉了 ANDROID_SDK_HOME配置的繁琐, 因为有时候配置无效; */ public class AndroidDoc { private static final String TAG = "<script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>"; //修改下边这个doc_path为自己使用的sdk doc路径 private static String doc_path = "D:\\android_tools\\android-sdk_r20.0.3-windows\\android-sdk-windows\\docs"; public static void main(String[] args) { File docs = new File(doc_path); System.out.println("It will take several minutes, please wait..."); traverse(docs); System.out.println("it is done... you can open the index.html faster!"); } /** * 遍历Android SDK安装目录下的docs目录 * * @param dir */ private static void traverse(File dir) { if (dir.isDirectory()) { File[] files = dir.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isFile() && file.getName().endsWith(".html")) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String buffer; while ((buffer = reader.readLine()) != null) { if (buffer.trim().equalsIgnoreCase(TAG)) { return true; } } } catch (Exception e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } else if (file.isDirectory()) { return true; } return false; } }); for (File file : files) { if (file.isDirectory()) { System.out.println("Scanning Folder:" + file.getAbsolutePath()); traverse(file); } else { doChange(file); } } } } /** * 注释html中的某行Javascript代码 * * @param file * @return */ private static boolean doChange(File file) { System.out.println("Extracting File:" + file.getAbsolutePath() + "..."); String desPath = file.getParent() + "\\" + UUID.randomUUID().toString() + ".html"; File des = new File(desPath); BufferedReader reader = null; BufferedWriter writer = null; try { des.createNewFile(); reader = new BufferedReader(new FileReader(file)); writer = new BufferedWriter(new FileWriter(des)); String buffer; while ((buffer = reader.readLine()) != null) { if (buffer.trim().equalsIgnoreCase(TAG)) { buffer = "<!--" + TAG + "-->"; } writer.write(buffer + "\n"); writer.flush(); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); writer.close(); file.delete(); des.renameTo(file); } catch (IOException e) { e.printStackTrace(); } } return false; } }