2013-01-19 62 views
11

Tôi đang sử dụng API Java 7 File. Tôi đã viết một lớp đó đang làm việc tốt trên Ubuntu tạo thư mục một cách hoàn hảo, nhưng khi tôi chạy cùng một mã trên Windows sau đó nó được ném lỗi:java.lang.UnsupportedOperationException: 'posix: permissions' không được hỗ trợ như thuộc tính ban đầu trên Windows

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) 

đang Thư mục lớp tôi là

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<Set<PosixFilePermission>> attr; 


    public Folder(UserPrincipal owner,Path folder_name,FileAttribute<Set<PosixFilePermission>> 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); 

    } 
} 

Lỗi này là đến từ createFolder phương thức Folder.

Làm cách nào để giải quyết lỗi này?

+2

Windows! = Posix. Tại sao bạn mong đợi điều này để làm việc? – EJP

Trả lời

15

Bạn sử dụng PosixFilePermission mà có thể được sử dụng với hệ điều hành mà là compatibile với 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

của Windows unfortunatelly không hỗ trợ hệ thống tập tin POSIX vì vậy đây là lý do tại sao mã của bạn không hoạt động. Để tạo một thư mục trong Windows, bạn nên sử dụng:

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

Các / sẽ được tự động chuyển thành \ trong Windows. Nếu bạn muốn tạo toàn bộ đường dẫn cùng một lúc, bạn phải sử dụng phương thức mkdirs(). Thông tin thêm: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

Để đặt quyền tệp trong Windows, bạn phải sử dụng setReadable(), setWritable()setExecutable(). Đó là các phương thức lớp học File và chỉ đặt quyền của chủ sở hữu tệp. Lưu ý rằng các phương pháp được đề cập đã được thêm vào trong Java 1.6. Trong các phiên bản cũ hơn, bạn sẽ phải sử dụng (phiên bản Windows):

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

+0

vâng tôi muốn biết điều gì sẽ là mã để tạo thư mục trong cửa sổ !! –

+0

Xin chào, đừng khuyên bạn nên sử dụng 'File': ( – fge

+0

và một thứ nữa có thể là cuối cùng Làm thế nào để thay đổi tên của chủ sở hữu của tập tin trong đoạn mã trên? Xin vui lòng trả lời –

Các vấn đề liên quan