Hashmap没有存储到文件中?

时间:2014-03-03 15:50:24

标签: java eclipse file-io

当我调试下面的代码时,它会在FileNotFoundException上引发properties.store(fos, "none"),具体原因是什么?

编辑:通过堆栈跟踪,你的意思是以下,然后是:

Exception(Throwable).<init>() line: 249 [local variables unavailable]   
Exception.<init>() line: not available  
Thread.getStackTrace() line: not available  
JCP.WriteVariablesHashmap(HashMap<String,Object>) line: 51  
JCP.NewMethod(String[]) line: 133   
JCP.InteractiveMode(String[]) line: 165 
JCP.BatchOrInterActive(String[]) line: 96   
Main.main(String[]) line: 13    

代码:

public void WriteVariablesHashmap(HashMap<String, Object> hashMap){
    try {
        File file=new File("Variables.ser");
        FileOutputStream fos = null;

        Properties properties = new Properties();
        fos=new FileOutputStream(file);

        for (Map.Entry<String,Object> entry : hashMap.entrySet()) {
            properties.put(entry.getKey(), entry.getValue());
        }
        System.out.println(file);

        properties.store(fos, "none");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

Stacktrace会有所帮助。此外,您是否可以确认您的用户目录中是否具有读写权限,或者是否存在任何名为Variables.ser的文件。来自Javadocs 1.6

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