2013-01-19 43 views
11

पर प्रारंभिक विशेषता के रूप में समर्थित नहीं है I जावा 7 फ़ाइल API का उपयोग कर रहा हूं। मैं एक वर्ग है कि पूरी तरह से उबंटू बनाने निर्देशिकाओं पर ठीक काम कर रहा है लिखा था, लेकिन जब मैं विंडोज पर एक ही कोड चलाने तो यह त्रुटि फेंक है:java.lang.UnsupportedOperationException: 'posix: अनुमतियाँ' 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) 

मेरा फ़ोल्डर वर्ग कोड

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

    } 
} 

त्रुटि है Folder की विधि से आ रहा है।

मैं इस त्रुटि को कैसे हल करूं?

+2

विंडोज = Posix: पुराने संस्करणों में आप (Windows संस्करण) का उपयोग करना होगा। आपने यह काम करने की अपेक्षा क्यों की? – EJP

उत्तर

15

आप PosixFilePermission का उपयोग जो केवल ऑपरेटिंग सिस्टम जो POSIX के साथ compatibile हैं के साथ प्रयोग किया जा सकता है:

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

विंडोज Unfortunatelly फ़ाइल प्रणाली का समर्थन नहीं करता तो यह क्यों अपने कोड है काम नहीं करता आदेश Windows में एक निर्देशिका बनाने के लिए उपयोग करना चाहिए:

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

/ स्वचालित रूप से Windows में \ में बदल जायेगा। यदि आप एक बार में पूरा पथ बनाना चाहते हैं तो आपको mkdirs() विधि का उपयोग करना होगा। और जानकारी: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

Windows में फाइल अनुमति सेट करने के लिए setReadable(), setWritable() और setExecutable() उपयोग करने के लिए आदेश में। File कक्षा विधियां हैं और केवल फ़ाइल स्वामी की अनुमतियां सेट करें। ध्यान दें कि जावा 1.6 में उल्लिखित विधियों को जोड़ा गया था। !

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

+0

हाँ मैं जानना चाहता हूं कि विंडोज़ में निर्देशिका बनाने के लिए कोड क्या होना चाहिए !! –

+0

अरे, 'फ़ाइल' का उपयोग करने की अनुशंसा नहीं करें :( – fge

+0

और एक और शायद आखिरी बार उपरोक्त कोड में फ़ाइल के मालिक के नाम को कैसे बदला जाए ?? कृपया उत्तर दें !! –