Topic: 基于对象的读写的问题,菜鸟问题,望指教,谢谢!! |
Print this page |
1.基于对象的读写的问题,菜鸟问题,望指教,谢谢!! | Copy to clipboard |
Posted by: zhjdenis Posted on: 2007-11-01 19:03 我感觉编写没有什么问题,可是调用这个类的时候总是抛出。实现***能是将一个存着数据的hashmap对象存入recordtxt这个文件中(我定义的时候用的是个C:\123.txt);我使用时都前生成了该类的对象,并且我也看到确实存在着这个recordtxt被生成了。这个类中一共有3个方法,但是都会抛出该异常。java.io.FileNotFoundException: C:\zhjdenis.txt (拒绝访问。) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at 注册表修改界面.AboutRecord.<init>(AboutRecord.java:47) package 注册表修改界面; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; public class AboutRecord { Map<File, String> hashmap; Map<File, String> map2; File storetxt; public AboutRecord(File recordtxt){ storetxt=recordtxt; try { if(!storetxt.exists()) { storetxt.createNewFile(); hashmap=new HashMap<File, String>(); hashmap.put(storetxt, "locked"); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(storetxt)); out.writeObject(hashmap); out.flush(); out.close(); Runtime.getRuntime().exec("attrib "+storetxt+" +h"); } else { Runtime.getRuntime().exec("attrib "+storetxt+" +h"); ObjectInputStream in=new ObjectInputStream(new FileInputStream(storetxt)); try { map2=(HashMap)in.readObject(); in.close(); } catch (ClassNotFoundException e) { e.printStackTrace();} map2.put(storetxt, "locked"); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(storetxt)); out.writeObject(map2); out.flush(); out.close(); } } catch (IOException e1) { e1.printStackTrace();} } //将一个新的对应关系加入到hashmap并再次写入文件 void addnewoperationfile(String filename) { File file=new File(filename); try { ObjectInputStream in=new ObjectInputStream(new FileInputStream(storetxt)); map2=(HashMap)in.readObject(); in.close(); map2.put(file, "locked"); modifyregedit.changedcontent3.append(filename); modifyregedit.changedcontent3.append("\tlocked\n"); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(storetxt)); out.writeObject(map2); out.flush(); out.close(); } catch (FileNotFoundException e) { System.out.println("zhjdenis1");//这个一般会被打印,也就是说这有抛出的异常 } catch (IOException e) { System.out.println("zhjdenis2"); } catch (ClassNotFoundException e) { System.out.println("zhjdenis3"); } } //查看写入文件中的hashmap中的内容 public void showpastoperation() { try { if(!storetxt.exists()) return; ObjectInputStream in=new ObjectInputStream(new FileInputStream(storetxt)); map2=(HashMap)in.readObject(); in.close(); modifyregedit.changedcontent3.setText(""); for(Map.Entry<File, String> entry:map2.entrySet()) { File key=entry.getKey(); String value=entry.getValue(); modifyregedit.changedcontent3.append(key.getAbsolutePath()); modifyregedit.changedcontent3.append("\t"+value+"\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //将指定的数据从hashmap中删除,然后将更新后的hashmap写回文件 public void searchAndUnlock(String filename) { File file=new File(filename); System.out.println(filename); try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(storetxt)); map2=(HashMap)in.readObject(); in.close(); if(map2.containsKey(file)) { map2.remove(file); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(storetxt)); out.writeObject(map2); out.flush(); out.close(); Modifyattribution.UnModifyattribution(filename); modifyregedit.changedcontent3.append("\n已经成***解锁"+filename+"\n"); } else modifyregedit.changedcontent3.append("\n"+file.getAbsolutePath()+" 未被隐藏或非正确的文件名"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } |
2.Re:基于对象的读写的问题,菜鸟问题,望指教,谢谢!! [Re: zhjdenis] | Copy to clipboard |
Posted by: zhjdenis Posted on: 2007-11-02 11:30 拜托大家帮忙了。要是觉得太多就看其中任意一个方法的行了,主要看其中的关于对象读写的那段有什么问题。主要是不知道为什么会有无法找到文件的异常发生呢?通过构造函数生成文件的时候还正常,而且我明明也看到了生成的文件,可是调用其他的方法的时候却会抛出无法找到文件的异常 |
3.Re:基于对象的读写的问题,菜鸟问题,望指教,谢谢!! [Re: zhjdenis] | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-11-02 16:54 出现这个问题和你将文件属性设为隐藏有关系,
隐藏的时候用只能用input打开,不能用output打开。 FileNotFoundException的异常时从java.io.FileOutputStream的
中抛出的,这是个本地方法。 所以我认为在java中即要隐藏文件又要用output方式打开。没办法实现。 希望有高手给提供一个解决方案。 |
4.Re:基于对象的读写的问题,菜鸟问题,望指教,谢谢!! [Re: zhjdenis] | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-11-02 17:11 有一点不明白FileInputStream和FileOutputStream都调用了本地方法
为什么打开隐藏文件时,FileOutputStream抛出异常,而FileInputStream不抛异常。是不是我上面的结论是错的。 求高人指点一二。 |
5.Re:基于对象的读写的问题,菜鸟问题,望指教,谢谢!! [Re: zhjdenis] | Copy to clipboard |
Posted by: zhjdenis Posted on: 2007-11-02 18:30 我试了一下,果然解决了问题,可是我难道没有两全其美的办法么? 异常的问题我也想知道答案 |
6.Re:基于对象的读写的问题,菜鸟问题,望指教,谢谢!! [Re: andy_wang_5] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-11-02 22:30 andy_wang_5 wrote: Sounds like a fair solution for me. If a file is hidden in Windows, I assume we can still read that file in Java but not overwrite it. I am not sure about this solution but I am happy with it. Sounds ok to me. I would like somebody test the same code in Linux to see if it has the same functionality under Windows. Well spot, Andy, good job. Regards, Jiafan |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |