[英]FileNotFoundException (Access is denied) when trying to write to a new file
I am trying to write simple objects ( SimpleType
) into files, so that the files can be loaded later and the objects recreated. 我试图将简单对象(
SimpleType
)写入文件,以便稍后加载文件并重新创建对象。
I am currently working in the NetBeans IDE (JDK8) on a Windows 7 machine. 我目前正在使用Windows 7计算机上的NetBeans IDE(JDK8)。 I don't think that should make a difference, though.
不过,我认为这不应该有所作为。
This is the type I would like to write into the file: 这是我想写入文件的类型:
public class SimpleType implements Serializable {
boolean[] a;
boolean[] b;
}
This is the code I'm trying to get to run: 这是我试图运行的代码:
public class Test {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
String fileName = "test.txt";
SimpleType foo = new SimpleType;
try (ObjectOutputStream out = new ObjectOutputStream(new
BufferedOutputStream(new FileOutputStream(fileName)))) {
out.writeObject(foo);
out.close();
}
}
}
The code compiles and runs, but always throws a FileNotFoundException
: 代码编译并运行,但始终抛出
FileNotFoundException
:
Exception in thread "main" java.io.FileNotFoundException: test.txt (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at Test.main(Test.java:33)
public FileOutputStream(String name) throws FileNotFoundException
[...]
[...]
Parameters:
参数:
name
- the system-dependent filenamename
- 依赖于系统的文件名
Throws:抛出:
FileNotFoundException
- if the file exists but is a directory rather than a regular file, does not exist but cannot be created , or cannot be opened for any other reasonSecurityException
- if a security manager exists and its checkWrite method denies write access to the file.FileNotFoundException
- 如果文件存在但是是目录而不是常规文件, 则不存在但无法创建 ,或者由于任何其他原因无法打开SecurityException
- 如果存在安全管理器且其checkWrite方法拒绝对文件的写访问。
I am sure that I have read/write permissions in the directory; 我确信我在目录中有读/写权限; there is no existing file with the name test.txt so it cannot be locked by another program.
没有名为test.txt的现有文件,因此无法被其他程序锁定。
Changing fileName
to an absolute path I am sure I can write into doesn't make any difference. 将
fileName
更改为绝对路径我相信我可以写入没有任何区别。
It is reproducible if file is in read-only mode. 如果文件处于只读模式,则可以重现。 Can you try like this.
你能尝试这样吗?
public static void main(String[] args) {
String fileName = "sampleObjectFile.txt";
SampleObject sampleObject = new SampleObject();
File file = new File(fileName);
file.setWritable(true); //make it writable.
try(ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))){
outputStream.writeObject(sampleObject);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
If you are writing the file on OS disk you need admin privileges. 如果您在OS磁盘上写入文件,则需要管理员权限。 so avoid writing on OS disk.
所以避免在OS磁盘上写。
Normally this is because you are trying to write on a location not allowed by your FileSystem (for example in Windows7 you cannot write a new file in c:). 通常这是因为您尝试在文件系统不允许的位置写入(例如,在Windows7中,您无法在c :)中编写新文件。 Try to investigate where the program is trying to write using procmon from Microsoft's SysInternals .
尝试使用Microsoft的SysInternals中的 procmon来调查程序尝试编写的位置。 Add a new filter (path contains test.txt) and see what happens.
添加一个新的过滤器(路径包含test.txt),看看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.