2009-11-09 15:33:36 文件属性配置类 package cn.sist.file; public class FileProperty { private int sysProperty = 0; private int hiddenProperty = 0; private int readProperty = 0; private int arriveProperty = 0; private boolean changeAll = false; public FileProperty setChangeAll(boolean changeAll){ this.changeAll = changeAll; return this; } public boolean getChangeAll(){ return this.changeAll; } protected int getArriveProperty() { return arriveProperty; } public FileProperty addArriveProperty() { this.arriveProperty = 1; return this; } public FileProperty removeArriveProperty(){ this.arriveProperty = 2; return this; } protected int getHiddenProperty() { return hiddenProperty; } public FileProperty addHiddenProperty() { this.hiddenProperty = 1; return this; } public FileProperty removeHiddenProperty(){ this.hiddenProperty = 2; return this; } protected int getReadProperty() { return readProperty; } public FileProperty addReadProperty() { this.readProperty = 1; return this; } public FileProperty removeReadProperty(){ this.readProperty = 2; return this; } protected int getSysProperty() { return sysProperty; } public FileProperty addSysProperty() { this.sysProperty = 1; return this; } public FileProperty removeSysProperty(){ this.sysProperty = 2; return this; } } 文件修改类 package cn.sist.file; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class FilePropertyChange { private static final String xpCommand = "cmd /c attrib"; private static final String[] xpSysProperty = {"","+s","-s"}; private static final String[] xpHiddenProperty = {"","+h","-h"}; private static final String[] xpArriveProperty = {"","+a","-a"}; private static final String[] xpReadOnlyProperty = {"","+r","-r"}; /* * defaultFileProperty only have arriveProperty. * it has removed all property except arriveProperty */ public static final FileProperty defaultFileProperty = new FileProperty().addArriveProperty().removeSysProperty() .removeHiddenProperty().removeReadProperty(); /* * XPFileProertyChange is a method which execute under System of windows XP. * It use dos command to change the file's Property */ public static boolean XPFilePropertyChange(File file,FileProperty fp){ // if(!file.exists()) // return false; final String space = " "; StringBuffer sb = new StringBuffer().append(FilePropertyChange.xpCommand).append(space); sb.append(FilePropertyChange.xpArriveProperty[fp.getArriveProperty()]).append(space); sb.append(FilePropertyChange.xpHiddenProperty[fp.getHiddenProperty()]).append(space); sb.append(FilePropertyChange.xpReadOnlyProperty[fp.getReadProperty()]).append(space); sb.append(FilePropertyChange.xpSysProperty[fp.getSysProperty()]).append(space); sb.append("/"").append(file.getAbsolutePath()).append("/" "); sb.append("/s /d"); StringBuffer sb2 = null; try{ Runtime.getRuntime().exec(sb.toString()); }catch(IOException ioe){ System.out.println("Error"); ioe.printStackTrace(); return false; } if(fp.getChangeAll()) return XPFilePropertyChange(new File(file.getAbsoluteFile()+"//*"),fp.setChangeAll(false)); return true; } /* * @paramter file * @return List<File> */ public static List<File> listFile(File file){ if(!file.exists()) return null; List<File> list = new ArrayList<File>(); list.add(file); File[] files = null; if(file.isDirectory()){ files = file.listFiles(); for(File tmpFile:files){ if(tmpFile.isDirectory()){ list.addAll(listFile(tmpFile)); }else{ list.add(tmpFile); } } } return list; } public static void main(String[] args) { FileProperty fp = new FileProperty(); fp.removeSysProperty().removeHiddenProperty() .addArriveProperty().addReadProperty().setChangeAll(false); File file = new File("H://hidden"); System.out.println(file.getAbsolutePath()); boolean sucess = FilePropertyChange.XPFilePropertyChange(file, fp); System.out.println(sucess); } } 使用方法 public static void main(String[] args) { FileProperty fp = new FileProperty(); fp.removeSysProperty().removeHiddenProperty() .addArriveProperty().addReadProperty().setChangeAll(false); File file = new File("H://hidden"); System.out.println(file.getAbsolutePath()); boolean sucess = FilePropertyChange.XPFilePropertyChange(file, fp); System.out.println(sucess); }