NIO.2支持的视图

  • 视图分类
  1. BasicFileAttributeView: 这是一个基本属性视图,即所有系统必须支持它.这个属性视图的名称是"basic".
  2. DosFileAttributeView: 在支持DOS属性系统中,该视图提供了标准的四属性支持.这个属性视图的名称是"dos".
  3. PosixFileAttributeView: 在支持POSIX系列标准的系统中,该视图扩展了基本属性视图,如Unix.属性视图名称是"posix".
  4. FileOwnerAttributeView: 这种视图是支持的任何文件系统支持文件所有者的概念落实.该属性视图名称是"owner"
  5. AclFileAttributeView:这个视图支持读取或更新一个文件的ACL.该视图支持NFSv4模型.这个属性视图名称是"acl"
  6. UserDefinedFileAttributeView:此视图支持元数据,是用户定义的.
  • 确定系统支持的视图
    //获取文件系统
    FileSystem fs = FileSystems.getDefault();
    		
    //获取该文件系统支持的视图
    Set<String> views = fs.supportedFileAttributeViews();
    		
    for (String view : views) {
    	System.out.println(view);
    }


java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute at java.base/sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(WindowsSecurityDescriptor.java:358) at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:227) at java.base/java.nio.file.Files.newByteChannel(Files.java:371) at java.base/java.nio.file.Files.createFile(Files.java:648) at java.base/java.nio.file.TempFileHelper.create(TempFileHelper.java:137) at java.base/java.nio.file.TempFileHelper.createTempFile(TempFileHelper.java:160) at java.base/java.nio.file.Files.createTempFile(Files.java:913) at com.mt.demo.SonarCaseTest.createFileCaseFix(SonarCaseTest.java:84) at com.mt.demo.SonarCaseTest.testCreateFile(SonarCaseTest.java:59) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
最新发布
07-10
<think>我们正在处理一个Java异常:`java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute`。根据引用,这个异常通常发生在尝试在非POSIX兼容的系统(如Windows)上使用POSIX文件权限属性时。因为Windows不支持POSIX权限,所以当我们在Windows上运行依赖于POSIX权限的Java代码时,就会抛出此异常。 解决方案: 1. **检查操作系统**:首先确认你的程序是否需要在多平台运行。如果是,应该避免在非POSIX系统(如Windows)上使用POSIX文件权限操作。 2. **使用条件判断**:在尝试设置POSIX权限之前,检查操作系统是否支持。可以使用`FileSystems.getDefault().supportedFileAttributeViews().contains("posix")`来判断。 3. **替代方案**:在Windows系统上,可以使用其他方式设置文件权限,例如使用`java.nio.file.attribute.AclFileAttributeView`。 示例代码: 下面是一个设置文件权限的示例,它首先检查POSIX支持,如果支持则使用POSIX权限,否则在Windows上使用ACL(访问控制列表)设置权限(但请注意,ACL设置较为复杂,通常需要管理员权限)。 然而,由于问题中提到的异常是在设置初始属性时发生的,可能是在创建文件时尝试设置POSIX权限。因此,我们可以避免在Windows上传递这些属性。 修改前的代码可能类似于: ```java Path path = Paths.get("file.txt"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r--r--"); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Files.createFile(path, attr); // 在Windows上会抛出异常 ``` 修改后: ```java Path path = Paths.get("file.txt"); if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r--r--"); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Files.createFile(path, attr); } else { // 在Windows上,我们创建文件时不设置POSIX权限,因为不支持 Files.createFile(path); // 如果需要设置只读等属性,可以使用DOS属性 // 例如设置为只读(注意:这不同于POSIX权限,但可能是你想要的) Files.setAttribute(path, "dos:readonly", true); } ``` 注意:DOS属性中,我们可以设置"readonly"(只读)、"hidden"(隐藏)等,但无法精确设置类似POSIX的rwx权限。如果你需要更复杂的权限控制(如多用户/组权限),在Windows上需要使用ACL,这比较复杂。 4. **使用第三方库**:如果需要在不同操作系统上处理文件权限,可以考虑使用第三方库,如Apache Commons IO或Guava,它们可能提供了跨平台的权限设置方法。 5. **修改程序逻辑**:如果程序只运行在Windows上,则完全避免使用POSIX权限相关的代码。 根据引用[1]和[2],这个错误通常是因为在Windows上尝试使用POSIX权限导致的,所以解决方案主要是避免在Windows上使用这些操作。 另外,引用[3]提到`UnsupportedOperationException`通常是因为调用了未实现或不支持的方法,因此我们需要确保在调用方法前检查其支持性。 引用[4]提到了一个`UnsatisfiedLinkError`,但我们的问题是`UnsupportedOperationException`,所以不直接相关。 因此,总结解决方案:**通过检查文件系统是否支持POSIX属性来避免在非POSIX系统上使用POSIX权限**。 相关问题: 1. 如何在Java中检查文件系统是否支持POSIX权限? 2. 在Windows上如何设置文件权限? 3. Java中如何处理跨平台的文件权限设置?</think>### Java中`UnsupportedOperationException: 'posix:permissions'`异常解决方案 这个异常发生在**非POSIX兼容系统**(如Windows)上尝试使用POSIX文件权限属性时。以下是根本原因和解决方案: #### 原因分析 1. **平台不兼容** POSIX权限(如`rwxr-xr-x`)是Unix/Linux系统的特性,Windows文件系统不支持此属性[^1][^2]。 2. **错误使用场景** 在Windows上调用以下方法会触发异常: ```java Path path = Paths.get("file.txt"); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r--r--"); Files.createFile(path, PosixFilePermissions.asFileAttribute(perms)); // 此处抛出异常 ``` #### 解决方案 ##### 方法1:检查操作系统支持(推荐) ```java Path path = Paths.get("file.txt"); FileSystem fs = FileSystems.getDefault(); if (fs.supportedFileAttributeViews().contains("posix")) { // POSIX系统(Linux/macOS) Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-r--r--"); Files.createFile(path, PosixFilePermissions.asFileAttribute(perms)); } else { // Windows系统 Files.createFile(path); // 使用DOS属性替代 Files.setAttribute(path, "dos:readonly", true); // 设置只读属性 } ``` ##### 方法2:使用跨平台权限控制 ```java // 设置所有用户可读(跨平台方案) path.toFile().setReadable(true, false); // false表示不限制所有者 // 设置只读(Windows兼容) path.toFile().setWritable(false); ``` ##### 方法3:避免直接使用POSIX属性 ```java // 仅创建文件,不设置初始权限 Files.createFile(Paths.get("file.txt")); // 后续通过标准API修改属性 Files.setPosixFilePermissions(path, perms); // 此方法在Windows调用仍会失败! // 应替换为: Files.setAttribute(path, "dos:readonly", true); // Windows专用属性 ``` #### 关键注意事项 1. **平台检测必须前置** 调用POSIX API前务必用`supportedFileAttributeViews()`检测[^3]。 2. **Windows替代属性** | POSIX权限 | Windows等效操作 | |---------------|-------------------------| | `rw-r--r--` | `setReadable(true)` | | `rw-------` | `setReadable(true, true)`(仅所有者)| | 设置可执行权限 | 无直接等效,需用`.bat`扩展名关联 | 3. **第三方工具** 考虑使用Apache Commons IO的`FileUtils`处理跨平台权限: ```java FileUtils.touch(new File("file.txt")); FileUtils.setReadable(new File("file.txt"), true); ``` #### 预防措施 - 在Windows开发环境中彻底避免`PosixFilePermissions`类 - 使用`java.nio.file.attribute`包前检查属性支持性: ```java boolean supportsPosix = FileSystems.getDefault() .supportedFileAttributeViews() .contains("posix"); ``` > **提示**:此异常本质是平台API不匹配问题,通过条件分支处理不同系统特性可彻底解决[^1][^3]。 --- ### 相关问题 1. 如何在Java中检测当前操作系统类型? 2. Windows系统下有哪些替代POSIX权限的文件控制方法? 3. Java NIO文件操作中如何处理跨平台路径分隔符问题? 4. `java.nio.file.attribute`包支持哪些非POSIX文件属性视图? [^1]: POSIX权限在Windows系统上不被支持 [^2]: Windows文件系统与POSIX标准的兼容性限制 [^3]: Java跨平台文件属性操作的最佳实践
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值