import java.io.*; import java.util.*; import java.lang.*; import java.text.*; public class triproot { static ArrayList dirlist = new ArrayList(); static HashSet filewriter=new HashSet(); static long filenum=0; static long dirnum=0; public String getDateString(long unixtime) //convert unix time to human time { Date date = new Date(unixtime); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(date); return dateString; } void detectwhatisit(String receivefilename) //detect receivefilename is a dir or a file,push dir to dirlist,push file to filewriter; { File dir2 = new File(receivefilename); boolean isDir = dir2.isDirectory(); if (isDir) { triproot.dirnum++; triproot.dirlist.add(receivefilename); } else { triproot.filenum++; String lastmodified=getDateString(dir2.lastModified()); triproot.filewriter.add("Size : "+dir2.length() +"\t Last modify : "+lastmodified + " \t File name :"+dir2.getPath() ); }//else end }//end of detectwhatisit boolean listfile(String getDir) //scan getDir { File dir = new File(getDir); String[] children = dir.list(); boolean list_if=false; if (children == null) { // Either dir does not exist or is not a directory System.out.println("Directory is null"); } else { for (int i=0; i<children.length; i++) { // Get filename of file or directory String filename = children[i]; detectwhatisit(getDir+"\\"+filename); //if os is linux,replace "\\" to "/" if((i+1)==children.length) { list_if=true; } else{list_if=false;} //if scan getDir is done,set list_if=true }//for end }//else end return list_if; }//end of listfile void init_dir(String original,String outDir) { ArrayList templist=new ArrayList(); if(new triproot().listfile(original)) { while(dirlist.size()!=0) { for(int j=0;j<dirlist.size();j++) { templist.add(dirlist.get(j).toString()); if((j+1)==dirlist.size()) dirlist.clear(); //copy dirlist to templist,and clear dirlist } for(int k=0;k<templist.size();k++) //scan the second round dir { new triproot().listfile(templist.get(k).toString()); if((k+1)==templist.size()){ templist.clear(); } else{} } if(dirlist.size()==0) { try { PrintWriter out2 = new PrintWriter(new FileWriter(outDir)); Iterator ir=filewriter.iterator(); while(ir.hasNext()) { out2.println(ir.next()); } System.out.println("Total dirs is: "+triproot.dirnum); System.out.println("Total files is: "+triproot.filenum); out2.close(); } catch(Exception e) { System.out.println(e); } } }//end of while } }//end of function init_dir void check_dir(String newList,String oldList) { try { BufferedReader new_List =new BufferedReader(new FileReader(newList)); BufferedReader old_List =new BufferedReader(new FileReader(oldList)); HashSet new_hash=new HashSet(); HashSet old_hash=new HashSet(); String s=new String(); while((s = new_List.readLine())!= null) { new_hash.add(s); } new_List.close(); while((s = old_List.readLine())!= null) { old_hash.add(s); } old_List.close(); File delete_temp = new File(newList); delete_temp.delete(); Iterator irq=new_hash.iterator(); int countChange=0; System.out.println("-----------Start Check-----------------------------------------------------"); while(irq.hasNext()) { s =(String)irq.next(); if(old_hash.contains(s)) { } else { System.out.println(s); countChange++; } } System.out.println("-----------Check Done!-----------------------------------------------------"); System.out.println("Total "+countChange+ " Files have been Modified!!!"); } catch(Exception e) { System.out.println(e); } }//end of function check_dir public static void main(String args[]) { try{ if(args.length!=3) { if(args[0].equals("-help")) { System.out.println("User Guide :"); System.out.println("java triproot -init [Dir you want init] [Output file name]"); System.out.println("java triproot -check [Dir you want check] [Trip file,that must be init early]"); System.out.println("IF Dir include space,don't forget \"\" !"); System.exit(1); } if(args[0].equals("-check")||args[0].equals("-init")) { System.out.println("Incompleted :"); System.out.println("Please type \"java triproot -help\" for more infomation!"); System.exit(1); } System.out.println("Unkonw command "+ args[0]); System.out.println("Please type \"java triproot -help\" for more infomation!"); System.exit(1); } } catch(Exception e) { System.out.println(e); System.out.println("catched input error"); } if(args[0].equals("-init")) { new triproot().init_dir(args[1],args[2]); System.exit(0); } if(args[0].equals("-check")) { String temp_out="temp.out"; new triproot().init_dir(args[1],temp_out); new triproot().check_dir(temp_out,args[2]); System.exit(0); } else { System.out.println("Unkonw command "+ args[0]); System.out.println("Please type \"java triproot -help\" for more infomation!"); System.exit(1); } }//end of main }//end of class |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |