package com; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.net.URL; public class txt { //保存路径 private static String basePath = "E://"; /** * @param args */ public static void main(String[] args) { String urls = "http://ajava.org/course/java/"; URL uls; try { //校验URL 的合法性 if(true){ uls = new URL(urls); basePath += uls.getHost(); File file = new File(basePath); if(!file.isDirectory()){file.mkdir();} for(int i= 12487;i<17920;i++){ uls = new URL(urls+i+".html"); alysisUrl(uls); } //alysisUrl(uls); } } catch (Exception e) { System.out.println("Error:"+e.getMessage()); } } //URL解析 public static void alysisUrl(URL url){ try { //目录 String path = basePath+url.getFile().substring(0, url.getFile().lastIndexOf("/"))+"/"; File file = new File(path); if(!file.isDirectory()){file.mkdirs();} //文件 String fileName = url.getFile().substring(url.getFile().lastIndexOf("/")+1); if("".equals(fileName)){ fileName = "index.html"; }else{ if(fileName.lastIndexOf(".") > 0){ //fileName = fileName.substring(0,fileName.lastIndexOf(".")) + ".html"; fileName = fileName.substring(0,fileName.lastIndexOf("."))+System.currentTimeMillis()+".html"; }else{ fileName = fileName.substring(0) + ".html"; } } /*查找文件,如果不存在,就创建*/ File newFile = new File(path+fileName); if(!newFile.exists()){ //newFile.createNewFile(); //读取内存流数据,保存到文件 String currentLine = null; StringBuffer sb = new StringBuffer(); BufferedWriter out = new BufferedWriter(new FileWriter(path+fileName)); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); while((currentLine = reader.readLine()) != null) { sb.append(currentLine); } reader.close(); out.write(sb.toString()); out.newLine(); out.close(); } System.out.println(path + " "+fileName); } catch (Exception e) { System.out.println("Error:"+e.getMessage()); } } //URL校验 public static boolean checkUrl(String url){ try { String fileName = url.substring(url.lastIndexOf("/")+1); if(fileName.indexOf(".") > 0){ if(".html".equals(fileName.substring(fileName.lastIndexOf(".")))){return true;} if(".shtml".equals(fileName.substring(fileName.lastIndexOf(".")))){return true;} return false; }else{return true;} }catch (RuntimeException e) {return false;} } }