2014-02-25 68 views
5

我有以下與iText庫正確集成的代碼。FileOutputStream訪問被拒絕:JAVA

import java.io.*; 
import com.itextpdf.text.*; 
import com.itextpdf.text.pdf.PdfWriter; 

@org.eclipse.jdt.annotation.NonNullByDefault(true) 
public class HelloWorld {  
    public static final String RESULT = "C:\\Users\\administrator\\Pictures\\tuto"; 

    @SuppressWarnings("resource") 
    public static void main(String[] args) throws DocumentException, IOException { 
     Document document = new Document(); 
     PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 
     document.open(); 
     document.add(new Paragraph("Hello World!")); 
     document.close(); 
    } 
} 

此代碼返回錯誤消息,如下所示。

Exception in thread "main" java.io.FileNotFoundException: C:\Users\valentin.schaefer\Pictures\tuto (Access is denied) 
    at java.io.FileOutputStream.open(Native Method) 
    at java.io.FileOutputStream.<init>(Unknown Source) 
    at java.io.FileOutputStream.<init>(Unknown Source) 
    at HelloWorld.main(HelloWorld.java:25) 

然而,我是計算機管理員,我通常擁有所有的權限帳戶。我不明白他爲什麼讓我退縮Access is denied

+0

該目錄是否存在? – pitseeker

+0

您是否嘗試從用戶空間以外的某處訪問文件?或不同的驅動器? – sakura

+0

是的,該目錄存在,該文件夾擁有傻瓜權限 – mortiped

回答

1

您需要獲得訪問該文件位置的權限。有兩種可能的解決方案。

1. use deferent file location to store your file (eg: D:\\somewhere) 
2. make sure that you have permission to access current location by granting 
    read write permissions. 
+0

該文件位置已具有完全權限 – mortiped

0

一看其實你正在嘗試使用FileOutputStream() 訪問目錄意味着您正試圖訪問目錄「C:\ Users \ administrator \ Pictures \ tuto」 using -

public static final String RESULT = "C:\\Users\\administrator\\Pictures\\tuto"; 
new FileOutputStream(RESULT); 

這是錯誤的,因爲可以提供給FileOutputstream()的有效輸入是文件名(如「xyz.txt」)或文件路徑(如「C:\ sample \ xyz.txt」)。

FileOutputstream()使用文件名或文件路徑,您的問題將解決。

謝謝。

0

我有一個類似的問題,我解壓縮了一個由於此錯誤消息而失敗的jar文件。這個jar是一個具有依賴關係的jar,我最近添加了一個新的依賴項。在檢查jar內容後,我發現有一個LICENSE文件和一個文件夾license在同一個根目錄下。雖然這在Linux上是完全有效的,但Windows文件系統將會出現故障。在我的情況下,解決這個錯誤的方法是在try/catch中。在catch中,檢查你是否在窗戶上,如果是這樣,記錄警告,因爲沒有太多可以做的事情,否則拋出。