如何更有效地组织、处理CSDN技术资料及文档(原创,附完整java源程序)
我们知道,CSDN上面有很多精彩的文章。我们在平常浏览(或通过查询)的时候,可以随时将文章保存到本地硬盘。但是,当我们查询出很多文章时候,如果每一页都打开,再另存到硬盘上,很浪费时间。 有的读者就采用netant等工具进行批量下载,但是文章下载回来之后,我们发现文章的标题并不是我希望的标题。如果我们采用网页的标题,就能根据标题有选择的进行阅读文章。 我用java写了一个工具,将从CSDN下载的文章,文件名为:Read_Article.aspId=xxx,我们从文件名很难看出其内容。因此,需要将其改名为网页的标题。 类名为:setHtmlFileName 用法为:java setHtmlFileName yourdocpath 你可以不写文档的路径,系统会采用当前路径。 在处理时,采用直接将文件改名的方法。所以,在操作前,请做好原文件的备份。 我使用的系统是:win2k+j2sdk1.4.0 时间:2003-10-26 15:31 如果你在使用的时候遇到问题,请与我联系。 email:[email protected] QQ:19843788 CSDN nickname:hajavaor 程序特色: 1、用户提供目录,自动将该目录下非htm的文件改为网页的标题。 2、程序仅执行改名的操作,因此效率较高。 3、充分考虑了文件名的合性性的验证 4、有重复文件,重新命名文件,格式为:原文件名+.复制_x.htm
附源文件: <pre> //author: hajavaor(CSDN nickname) //name:caozhaoyong, Beijing //date:2003-10-26 15:31 //mail:[email protected] //QQ:19843788 //file name:setHtmlFileName.java
import java.io.*; public class setHtmlFileName{ public static void main(String args[]) { String strUserPath = System.getProperty("user.dir"); File dir=null; String strFilePath=null;
if (args.length!=0){ strUserPath = args[0].trim(); }
try{ dir = new File(strUserPath); } catch(Exception ex){ System.err.println("error:"+ex.getMessage()); System.exit(0); } String strWorkingPath=null; if (dir.getAbsoluteFile().getParent()==null){ strWorkingPath = dir.getAbsoluteFile().getParent()+"\\"+ dir.getPath(); } else{ strWorkingPath = dir.getPath(); } System.out.println("Working directory is:"+strWorkingPath); System.out.println("================================");
if (dir.isDirectory()==false){ System.out.println("Bad directory. Consult to the Usage."); System.exit(0); }
File files[]=null; files = dir.listFiles(); BufferedReader br = null; String strFileContent=null; String strMid =null; String strFileName=null; String strFileFullName=null; int iFileBegin=-1; int iFileEnd=-1; File newFile=null; int iFilesChanged=0; for(int i=0;i<files.length;i++){ if ((files[i].isFile()) && (!files[i].getName().endsWith(".htm"))){ try{ System.out.print("*"+files[i].getName()+"-->"); strFileName=""; strFileContent=""; br = new BufferedReader(new FileReader(files[i])); strMid=""; while ((strMid=br.readLine())!=null){ strFileContent+=strMid; if (strFileContent.indexOf("</title>")!=-1){break;} strMid=br.readLine(); } br.close(); //read title iFileBegin = strFileContent.indexOf("<title>"); iFileEnd=strFileContent.indexOf("</title>"); if ((iFileBegin!=-1) &&(iFileEnd !=-1)){ strFileName = strFileContent.substring(iFileBegin+7,iFileEnd); strFileName=strFileName.replaceAll("CSDN_文档中心_",""); //replace some un-support chars in the file name. strFileName=strFileName.replaceAll("<","《"); strFileName=strFileName.replaceAll(">","》"); strFileName=strFileName.replaceAll("\"","“"); strFileName=strFileName.replaceAll(":",":"); strFileName = replaceString(strFileName,'\\',"、"); strFileName = replaceString(strFileName,'/',"/"); strFileName = replaceString(strFileName,'*',"*"); strFileName = replaceString(strFileName,'?',"?"); strFileName = replaceString(strFileName,'|',"|"); //begin to manipulate files. strFileFullName=strWorkingPath+"\\"+strFileName; newFile = new File(strFileFullName+".htm"); if (newFile.exists()){ strFileName+=".复制_"; for(int j=1;;j++){ strFileFullName =strWorkingPath +"\\"+ strFileName+j; newFile = new File(strFileFullName+".htm"); if (newFile.exists()==false) { strFileName+=j; break; } } } System.out.print(strFileName+".htm--"); if (files[i].renameTo(new File(strFileFullName+".htm"))){ System.out.println("ok."); } else{ System.out.println("failed."); } iFilesChanged++; } } catch(Exception ex){ System.out.print("failed. err:"+ex.getMessage()); } } } System.out.println("================================"); System.out.println("Job finished. Changed Files:"+iFilesChanged);
}
public static String replaceString(String strSource,char char1,String str){ while (strSource.indexOf(char1)!=-1){ strSource=strSource.replace(char1,'~'); strSource=strSource.replaceAll("~",str); } return strSource; } }
//---the end of the program.
</pre> 
|