IOException at java.io.File.createNewFile(); [英] IOException at java.io.File.createNewFile();

查看:270
本文介绍了IOException at java.io.File.createNewFile();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一些无法序列化的代码.我试图在if语句中插入CanRead()和CanWrite(),这表明它们没有读写权限.我也曾尝试将'java.io.File.setReadable'和'java.io.File.setWriteable'插入为true,但仍然会引发错误.

I do have some code for serialization which does not work. I have tried to insert both CanRead() and CanWrite() in if-statements which showed they did not have permissions to read and write. I have also tried to insert 'java.io.File.setReadable' and 'java.io.File.setWriteable to true but it still throws the error.

代码如下:

public static void save(Object obj, String filename) throws FileNotFoundException, IOException
{
    File f = new File("c:/DatoChecker/" + filename);
    File dir = new File("c:/DatoChecker");
    if(!dir.exists())
        dir.mkdirs();
    f.setReadable(true);
    f.setWritable(true);
    if(!f.exists())
    {
        f.createNewFile();
    }
    FileOutputStream op = null;
    ObjectOutputStream objectStream = null;
    op = new FileOutputStream(f);
    objectStream = new ObjectOutputStream(op);
    objectStream.writeObject(obj);
    objectStream.close();
}

public static Object fetch(String filename) throws FileNotFoundException, IOException, ClassNotFoundException
{
    File f = new File("c:/DatoChecker" + filename);
    File dir = new File("c:/DatoChecker");
    if(!dir.exists())
        dir.mkdirs();
    f.setReadable(true);
    f.setWritable(true);
    if(!f.exists())
    {
        f.createNewFile();
    }
    FileInputStream ip = null;
    ObjectInputStream objectStream = null;
    Object obj = null;
    ip = new FileInputStream(f);
    objectStream = new ObjectInputStream(ip);
    obj = objectStream.readObject();
    ip.close();
    objectStream.close();  
    return obj;
}

Stacktrace:

Stacktrace:

SEVERE: null
java.io.IOException: access denied
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:947)
    at com.check.me.Serialization.fetch(Seralization.java:39)
    at com.check.me.GoodsList.load(GoodsList.java:82)
    at com.check.me.START.main(START.java:22)

要保存的一个是从GoodsList(只是保存而不是加载)中创建的,但是在下面它要长很多,所以我暂时将其保留.

The one for save is congruet from GoodsList (just save instead of load) and up but it is quite a bit longer below so I will leave it out for now.

感谢您的帮助
Highace2

Thanks for the help beforehand
Highace2

推荐答案

您声明您没有拥有读写权限.而且,的确,您会看到一条错误消息,告诉您您没有权限.您需要在创建文件的目录上更改ACL,或选择其他目录.

You state that you did not have permission to read or write. And, indeed, you get an error telling you that you don't have permission. You need to change the ACL on the directory in which you are creating the file, or pick a different directory.

这篇关于IOException at java.io.File.createNewFile();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆