[Java]±È½ÏÁ½¸öĿ¼µÄÒìͬ£¨Òþ²Ø×ÓĿ¼ÒÔ¸ÄĿ¼ÏµÄÎļþºöÂԱȽϣ©
Ö÷ÒªÏë±È½ÏÎļþµÄ´óСºÍ×îºóµÄ¸üÐÂʱ¼ä£¬Èç¹ûÁ½Õ߶¼Ò»ÑùÔòÈÏΪһÑù¡£·ñÔòÈÏΪ²»Ïàͬ¡£
Ê×Ïȶ¨ÁËÒ»¸öÎļþÀàFileEntity.javaÀ´´æ´¢Ò»Ð©ÎļþËù±ØÒªµÄÊôÐÔ¡£
/** * * @author Phonnie * */ public class FileEntity { private String name; private String path; private Date lastModified; private String size; private boolean isHidden; private boolean isFile; private boolean isDirectory; /** * ´ú±íĿ¼»òÕßÎļþËùÔڵIJ㼶£¬ÆäʵĿ¼Ϊ0 */ private int level; public FileEntity() { } public FileEntity(String name, String path, Date lastModified, String size, boolean isHidden, boolean isFile,boolean isDirectory) { this.name = name; this.isDirectory = isDirectory; this.isFile = isFile; this.isHidden = isHidden; this.lastModified = lastModified; this.path = path; this.size = size; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public Date getLastModified() { return lastModified; } public void setLastModified(Date lastModified) { this.lastModified = lastModified; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public boolean isHidden() { return isHidden; } public void setHidden(boolean isHidden) { this.isHidden = isHidden; } public boolean isFile() { return isFile; } public void setFile(boolean isFile) { this.isFile = isFile; } public boolean isDirectory() { return isDirectory; } public void setDirectory(boolean isDirectory) { this.isDirectory = isDirectory; } public void setLevel(int level) { this.level = level; } public int getLevel() { return level; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Name:" + name + ",\n"); sb.append("Path:" + path + ",\n"); sb.append("IsFile:" + isFile + ",\n"); sb.append("Hidden:" + isHidden + ",\n"); sb.append("IsDirectory:" + isDirectory + ",\n"); sb.append("Size:" + size + ",\n"); sb.append("Level:"+level+"\n"); sb.append("LastModified:"+ DateHelper.format(lastModified, "yyyy-MM-dd HH:mm:ss\n")); return sb.toString(); } public boolean equals(FileEntity o) { if ( o == null ) { return false; } else { if(this.isDirectory() && o.isDirectory()) { return o.getLastModified().getTime() == this.getLastModified().getTime(); } else if(this.isFile() && o.isFile()) { return this.getSize().equals(o.getSize()) && this.getLastModified().getTime() == o.getLastModified().getTime(); } return false; } } }È»ºó¶¥Ò»¸öÎļþ²Ù×÷ÀàÀ´²Ù×÷Îļþ
/** * * @author Phonnie * */ public class FileHelper { private static Logger _logger = Logger.getLogger(FileHelper.class.getName()); public static String getFileSize(File file){ if(file.isDirectory()) { return null; } try { FileInputStream fis = new FileInputStream(file); int size = fis.available(); return String.format("%,d B", size); } catch (FileNotFoundException e) { e.printStackTrace(); _logger.info("File not found:"+file.getName()); } catch (IOException e) { e.printStackTrace(); _logger.info("IO Exception:" + e.getMessage()); } return null; } public static void traversingDir(File file,List<FileEntity> list) { String name = file.getName(); String path = file.getPath(); String size = getFileSize(file); Date lastModified = new Date(file.lastModified()); if (file.isFile()) { FileEntity o = new FileEntity(name, path, lastModified, size, file.isHidden(), true, false); list.add(o); } else { FileEntity o = new FileEntity(name, path, lastModified, size, file.isHidden(), false, true); list.add(o); File[] files = file.listFiles(); for (File f : files) { traversingDir(f, list); } } } public static void output(List<FileEntity> list) { for (FileEntity fileEntity : list) { System.out.println(fileEntity.toString()); } } public static Map<Integer,List<FileEntity>> doCompare(List<FileEntity> srcList, List<FileEntity> destList, String src, String dest) { Map<Integer,List<FileEntity>> map = new HashMap<Integer,List<FileEntity>>(); removeHiddens(srcList); removeHiddens(destList); for (FileEntity o : srcList) { if(o.getLevel()!=0) { if(map.containsKey(o.getLevel())) { map.get(o.getLevel()).add(o); }else { List<FileEntity> list = new ArrayList<FileEntity>(); list.add(o); map.put(o.getLevel(), list); } } } for (FileEntity o : destList) { if(o.getLevel() != 0) { if(map.containsKey(o.getLevel())) { FileEntity entity = getFileEntity(map.get(o.getLevel()),o); if(entity == null) { map.get(o.getLevel()).add(o); } else { doRemove(map.get(o.getLevel()),entity); } }else { List<FileEntity> list = new ArrayList<FileEntity>(); list.add(o); map.put(o.getLevel(), list); } } } return map; } private static void doRemove(List<FileEntity> list,FileEntity o) { FileEntity result = null; for (FileEntity entity : list) { if(o.equals(entity)) { result = entity; break; } } list.remove(result); } public static void removeHiddens(List<FileEntity> list) { List<FileEntity> hiddens = new ArrayList<FileEntity>(); for (FileEntity o : list) { if(o.isHidden()) { hiddens.add(o); } } List<FileEntity> result = new ArrayList<FileEntity>(); for (FileEntity o : hiddens) { for (FileEntity entity : list) { if(entity.getPath().indexOf(o.getName())>-1) { result.add(entity); } } } list.removeAll(result); } private static FileEntity getFileEntity(List<FileEntity> list,FileEntity o) { for (FileEntity entity : list) { if((o.isFile() && entity.isFile() || o.isDirectory() && entity.isDirectory())) { if(o.getName().equals(entity.getName())) { return entity; } } } return null; } public static void setLevels(List<FileEntity> list,String src) { for (FileEntity o : list) { setLevel(o, src); } } private static void setLevel(FileEntity o,String src) { if(src.equals(o.getPath())) { o.setLevel(0); }else { src = src.replace(File.separator, "/"); String path = o.getPath().replace(File.separator, "/"); int srcLength = src.split("/").length; int length =path.split("/").length; length -=srcLength; o.setLevel(length); } } } ½ÓÏÂÀïÊÇÁ½¸öÎļþĿ¼±È½Ï
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- java urlÖØд¼¼Êõ(UrlReWriter)
- [ת]JavaµÄÊý×飨Array£©¡¢Vector¡¢ArrayLis....
- jsp»·¾³ÏÂajaxÂÒÂëÎÊÌâµÄ½â¾ö
- JSP¼¼Êõ Ö¸Áî ÂÒÂëÎÊÌâ ÔËÐÐÔÀíºÍ¾Å´óÒþʽ¶ÔÏó web¿ª·¢µÄË....
- JAVA ˮӡ ͼƬ ÎÄ×Ö
- java¶þ½øÖÆ£¬ÔÂë¡¢·´Âë¡¢²¹Â롢λÔËËã
- Java WebÓ¦ÓÃÆô¶¯¼ä¸ôÖ´ÐеijÌÐò
- jspÉú³ÉxmlÎļþʾÀý
- javaÖÐÀà±äÁ¿¡¢Àà·½·¨¡¢ÊµÀý±äÁ¿¡¢ÊµÀý·½·¨
- JSPÒ³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ