java删除无权限的文件_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 :

file.canExecute(); – return true, file is executable; false is not.

file.canWrite(); – return true, file is writable; false is not.

file.canRead(); – return true, file is readable; false is not.

Set the file permission :

file.setExecutable(boolean); – true, allow execute operations; false to disallow it.

file.setReadable(boolean); – true, allow read operations; false to disallow it.

file.setWritable(boolean); – true, allow write operations; false to disallow it.

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();         }     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值