2

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

代码如下:

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

堆栈跟踪:

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 的 congruet(只是保存而不是加载),但它在下面要长一些,所以我现在将其省略。

事先感谢您的帮助
Highace2

4

1 回答 1

5

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.

于 2013-02-10T14:36:45.040 回答