简体   繁体   English

尝试写入新文件时,FileNotFoundException(拒绝访问)

[英]FileNotFoundException (Access is denied) when trying to write to a new file

My goal 我的目标

I am trying to write simple objects ( SimpleType ) into files, so that the files can be loaded later and the objects recreated. 我试图将简单对象( SimpleType )写入文件,以便稍后加载文件并重新创建对象。

My setting 我的设定

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

My problem 我的问题

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)

My attempts to fix it 我尝试修复它

  • According to the documentation, I would expect the file to be created if it doesn't exist already. 根据文档,如果文件不存在,我希望创建该文件。 I've thoroughly read the Javadoc for the method I attempt to use, an excerpt of which I cite here (emphasis mine): 我已经彻底阅读了我试图使用的方法的Javadoc,这是我在这里引用的一段摘录(强调我的):

public FileOutputStream(String name) throws FileNotFoundException

[...] [...]

Parameters: 参数:
name - the system-dependent filename name - 依赖于系统的文件名
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 reason SecurityException - 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.

相关问题 尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝) - FileNotFoundException (permission denied) when trying to write file to sdcard in Android 上传文件时java.io.FileNotFoundException(路径)(访问被拒绝) - java.io.FileNotFoundException (path)(Access is denied) when uploading file 使用JAXB编组到File时,接收FileNotFoundException(访问被拒绝) - Receiving FileNotFoundException (Access is denied) when using JAXB to marshal to File FileNotFoundException:从 .txt 文件加载数据时“访问被拒绝” - FileNotFoundException: “Access is denied” when loading data from .txt file 安装到C:/ Program Files时,Java应用程序无法写入/读取序列化文件-java.io.FileNotFoundException :(拒绝访问) - Java application cannot write/read serialization file when installed to C:/Program Files - java.io.FileNotFoundException: (Access is denied) 尝试将字符串从Web写入文件时出现FileNotFoundException(permission) - FileNotFoundException(permission) when trying to write string from web to file 尝试写入文件时,Tomcat权限被拒绝 - Tomcat permission denied when trying to write a file Java读/写访问异常FileNotFoundException(访问被拒绝) - Java read/write access exception FileNotFoundException (Access is denied) apache Derby - 在创建新数据库时获取java.io.FileNotFoundException:derby.log(拒绝访问) - apache Derby - getting java.io.FileNotFoundException: derby.log (Access is denied) when creating new database Jave FileNotFoundException(拒绝访问) - Jave FileNotFoundException(Access Denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM