2016-10-05 5 views
0

디렉토리를 만들려고 시도했지만 AccessDeniedException이 계속 발생합니다. 어떻게 액세스 할 수 있습니까?MacOS에서 AccessDeniedException

코드 :

String pathForMac = System.getProperty("user.home") + "Library\\Application Support\\Petatech\\Zindroid"; 
Path macPath = Paths.get(pathForMac);{ 

if (!Files.exists(macPath)) { 
    try { 
     Files.createDirectory(macPath); 
    } catch (Exception e) { 
     System.out.println(e); 
     JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE); 
     } 
    } 
} 

답변

0

당신은이 작업을 수행 할 권한 블록 API를 사용할 수 있습니다. System.getProperty (String key)는 권한 부족으로 인해 AccessDeniedException을 던질 수 있습니다.

String pathForMac = java.security.AccessController.doPrivileged(
    new java.security.PrivilegedAction<String>() { 
     public String run() { 
      return System.getProperty("user.home"); 
     } 
    }); 
//Append the rest to the String 
pathForMac += "Library\\Application Support\\Petatech\\Zindroid"; 
+0

사실 Paths.get (pathForMac), Files.exists (macPath) 및 Files.createDirectory (macPath)는 SecurityException을 throw 할 수 있습니다. 스택 추적을 확인하고 doPrivileged 블록에서 해당 문을 래핑하십시오. – Harold