JAVA unix / Win OS / 文件目录创建、删除 和 目录权限(file permission)

 1 文件目录创建、删除(适用于unix \ Win XP 系统  ;Win 7未测试)

       public static  Boolean createScrrenManagerFolder(String savePath){
        Boolean isBln = false;
        String   savePath =HttpServletRequest.getServletContext().getRealPath("");
        String lastsFolderPath = "";

       String partPathString = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HHmmss");// 设置日期格式
        String systime = df.format(new Date());
        String partPathString = systime.substring(0, systime.lastIndexOf("/")+1);
        try{
            if (savePath.indexOf("wtpwebapps") != -1) {//eclipse 调试状态下的项目路径
                int index1 = savePath.indexOf("\\.");
                int index2 = savePath.lastIndexOf("\\");
                lastsFolderPath = savePath.substring(0, index1) + savePath.substring(index2, savePath.length()) + "\\WebContent\\" ;
            } else {//war包运行状态下的项目路径
                lastsFolderPath = savePath + "/" ;
            }
            lastsFolderPath = lastsFolderPath +"resources/upload/picture/uuid/"+partPathString;//eg:E:\Tomcat7\vCat/2013/05/07/
            File fp = new File(lastsFolderPath);  
            // 创建目录
            if (!fp.exists()) {
                fp.mkdirs();// 目录不存在的情况下,创建目录。
            }
            isBln = true;
        }
        catch(Exception e){
            isBln = false;
            e.printStackTrace();
        }
        return isBln;
    }

**====================================//4 递归删除空目录(只删除空的四级目录:%path%/uuid/2013/06/21/)================================**
    public void delFolderFour(){      

            Integer folderDeep = 4 ;
            while (lastsPath.indexOf("/") != -1  && ( (folderDeep--) > 0) ) {
                if( "0".equals(delFolder(lastsPath.trim())) ){
                    lastsPath = lastsPath.substring(0,lastsPath.length()-1);
                    lastsPath = lastsPath.substring(0,lastsPath.lastIndexOf("/"));
                }
                else{
                    break;
                }
            }

}

 //    递归删除空目录
    public static String delFolder(String filePath) {
        String isBln = "0";//含有空目录
        File fp = new File(filePath);
        try{
            // 递归删除空目录
            if (fp.exists() && fp.isDirectory()) {
                File[] files = fp.listFiles();
                if (files.length == 0) {// 空文件夹
                    fp.delete();
                }
                else{
                    isBln = "1" ;// 不为空文件夹,直接跳出,不用检查此目录的上级目录是否为空文件夹
                }
            }
        }
        catch(Exception e){
            isBln = "2";//异常,直接跳出
        }
        return isBln;
    }

**========================================目录权限(file permission)=======================================**

 2 目录权限(file permission)

http://docs.oracle.com/javase/6/docs/api/java/io/File.html(API )

前言:

         In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal with it.

Check if the file permission allow :

  1. file.canExecute(); – return true, file is executable; false is not.
  2. file.canWrite(); – return true, file is writable; false is not.
  3. file.canRead(); – return true, file is readable; false is not.

Set the file permission :

  1. file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
  2. file.setReadable(boolean); – true, allow read operations; false to disallow it.
  3. file.setWritable(boolean); – true, allow write operations; false to disallow it.
  4. separatorChar

    public static final char separatorChar
    The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system propertyfile.separator. On UNIX systems the value of this field is'/'; on Microsoft Windows systems it is'\\'.

         In *nix system, you may need to configure more specifies about file permission, e.g set a 777 permission for a file or directory, however, Java IO classes do not have ready method for it, but you can use the following dirty workaround :

Runtime.getRuntime().exec("chmod 777 file");


2.1 操作java.io Class File 类:

WIN OS:如对1添加权限:

.......

// 创建目录

  if (!fp.exists()) {

    fp.mkdirs();// 目录不存在的情况下,创建目录。

   if( !fp.canRead() ){

      fp.setReadable(true);
   }
    if( !fp.canWrite() ){
      fp.setWritable(true);
    }
   if( !fp.canExecute() ){
     fp.setExecutable(true);
   }
 }

.......


2.2 操作java.io Class  FilePermission类:(未研究)      

http://www.cjsdn.net/doc/jdk50/java/security/Permission.html(API) 

2.3  在*nix系统中,配置文件权限,文件的权限为777. (java IO类没有相关方法)

public static void updatefilePermi(){
        if( -1 != System.getProperties().getProperty("os.name").toLowerCase().indexOf("windows") ){
            //1 windows OS:通过io File类对文件路径赋予读写权限

           //如上
        }
        //1 2 其它操作系统 :通过untime.getRuntime().exec()执行command对文件路径赋予读写权限 ,待验证后进行修正
        String filepath = "";
        String command = "chmod 777 " + filepath ;
        Runtime runtime = Runtime.getRuntime();
        try {
            Process exec = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




你可能感兴趣的:(JAVA,SE)