FileOutputStream 访问被拒绝:JAVA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22007890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileOutputStream access is denied : JAVA
提问by mortiped
I have the following code with the iText library properly integrated.
我有以下代码与正确集成的 iText 库。
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();
}
}
This code returns me an error message, which is as follows.
此代码向我返回一条错误消息,内容如下。
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)
Yet I am the computer administrator and I normally have all permissions account. I don't understand why he retourn me Access is denied
.
然而我是计算机管理员,我通常拥有所有权限帐户。我不明白他为什么要回我Access is denied
。
采纳答案by Kick
You are trying to access the directory. The parameter of the FileOutputStream should be a File
/ Path
object pointingto a file:
您正在尝试访问该目录。FileOutputStream 的参数应该是一个指向文件的File
/Path
对象:
FileOutputStream file = new FileOutputStream("path/file.txt");
File -------------------------------^
For more detail take a look on http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
有关更多详细信息,请查看http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
回答by Ruchira Gayan Ranaweera
You need to have permission to access that file location. There are two possible solutions.
您需要具有访问该文件位置的权限。有两种可能的解决方案。
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.
回答by Bhanu
Actually you are trying to access directory using FileOutputStream( ) means you are trying to access directory "C:\Users\administrator\Pictures\tuto" using -
实际上,您正在尝试使用 FileOutputStream( ) 访问目录意味着您正在尝试使用 - 访问目录“C:\Users\administrator\Pictures\tuto”
public static final String RESULT = "C:\Users\administrator\Pictures\tuto";
new FileOutputStream(RESULT);
Which is wrong as the valid input which can be provided to FileOutputstream( ) is either file name (like "xyz.txt") or path of file (like "C:\sample\xyz.txt").
这是错误的,因为可以提供给 FileOutputstream() 的有效输入是文件名(如“xyz.txt”)或文件路径(如“C:\sample\xyz.txt”)。
Use file name OR file path with FileOutputstream( ) and your problem will solve.
将文件名或文件路径与 FileOutputstream( ) 一起使用,您的问题就会解决。
Thanks.
谢谢。
回答by spy
I had a similar problem in which I unzipped a jar file which failed due to this error message. This jar was a jar with dependencies and I had recently added a new dependency. After examining the jar contents, it turned out I had a LICENSE
file and a folder license
in the same root. While this is completely valid on Linux, the Windows file system will barf. The work around in my case to was trap this error in a try/catch. In the catch, check if you're on windows, if so log warning as there's not much that can be done, otherwise throw.
我有一个类似的问题,我解压缩了一个由于此错误消息而失败的 jar 文件。这个 jar 是一个有依赖的 jar,我最近添加了一个新的依赖。检查 jar 内容后,结果发现我在同一个根LICENSE
目录license
中有一个文件和一个文件夹。虽然这在 Linux 上完全有效,但 Windows 文件系统会失败。在我的情况下,解决方法是在 try/catch 中捕获此错误。在捕获中,检查您是否在 Windows 上,如果是,则记录警告,因为没有太多可以做的事情,否则抛出。
回答by thiagola92
Not the answer from this question
不是这个问题的答案
I got the same exception because Windows is not case sensitive.
Trying to create one file named "test" and other named "TEST" will generate the same exception.
我得到了同样的例外,因为 Windows 不区分大小写。
尝试创建一个名为“test”的文件和另一个名为“TEST”的文件将产生相同的异常。
回答by Gunjan Sharma
You can try this:
你可以试试这个:
if(!file.canRead()){
file.setReadable(true);
}
FileOutputStream file = new FileOutputStream("path/file.txt");
回答by Gunjan Sharma
by this, you can change access to your file or folder dynamically. Note: this will work on Linux machine only.
这样,您可以动态更改对文件或文件夹的访问权限。注意:这仅适用于 Linux 机器。
private void filePermissions(File filePath) throws IOException {
Path path = Paths.get(filePath.toString());
Set<PosixFilePermission> perms = java.nio.file.Files.readAttributes(path, PosixFileAttributes.class)
.permissions();
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_WRITE);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_WRITE);
perms.add(PosixFilePermission.OTHERS_READ);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
java.nio.file.Files.setPosixFilePermissions(path, perms);
}