我确实有一些序列化的代码不起作用。我试图在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;
}
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的congruet(只是保存而不是加载)以及向上,但它下面的时间要长一点,所以我暂时不用了。
事先感谢您的帮助
Highace2
答案 0 :(得分:5)
您声明您不拥有读取或写入权限。而且,实际上,如果您没有获得许可,则会收到错误消息。您需要更改创建文件的目录上的ACL,或选择其他目录。