windows posix java_java.lang.UnsupportedOperationException: 'posix:permissions' not supported as ini...

问题

I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error:

Exception in thread "main" java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute

at sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(Unknown Source)

at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source)

at java.nio.file.Files.createDirectory(Unknown Source)

at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source)

at java.nio.file.Files.createDirectories(Unknown Source)

at com.cloudspoke.folder_permission.Folder.createFolder(Folder.java:27)

at com.cloudspoke.folder_permission.Main.main(Main.java:139)

My Folder class code is

package com.cloudspoke.folder_permission;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.attribute.FileAttribute;

import java.nio.file.attribute.PosixFilePermission;

import java.nio.file.attribute.UserPrincipal;

import java.util.Set;

public class Folder{

// attributes required for creating a Folder

private UserPrincipal owner;

private Path folder_name;

private FileAttribute> attr;

public Folder(UserPrincipal owner,Path folder_name,FileAttribute> attr){

this.owner=owner;

this.folder_name=folder_name;

this.attr=attr;

}

//invoking this method will create folders

public void createFolder(){

try {

//createDirectories function is used for overwriting existing folder instead of createDirectory() method

Files.createDirectories(folder_name, attr);

Files.setOwner(folder_name, owner);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("created Folder "+this.folder_name);

}

}

The error is coming from createFolder method of Folder.

How do I resolve this error?

回答1:

You use PosixFilePermission which can be used only with operating systems which are compatibile with POSIX:

A file attribute view that provides a view of the file attributes commonly associated with files on file systems used by operating systems that implement the Portable Operating System Interface (POSIX) family of standards.

Operating systems that implement the POSIX family of standards commonly use file systems that have a file owner, group-owner, and related access permissions. This file attribute view provides read and write access to these attributes

Windows unfortunatelly doesn't support POSIX file systems so this is why your code doesn't work. In order to create a directory in Windows you should use:

new File("/path/to/folder").mkdir();

The / will be automatically changed to \ in Windows. If you want to create the whole path at once you have to use mkdirs() method. More info: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

In order to set file permissions in Windows you have to use setReadable(), setWritable() and setExecutable(). That are File class methods and set only file owner's permissions. Note that mentioned methods were added in Java 1.6. In older versions you would have to use (Windows version):

Runtime.getRuntime().exec("attrib -r myFile");

来源:https://stackoverflow.com/questions/14415960/java-lang-unsupportedoperationexception-posixpermissions-not-supported-as-in

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值