【问题标题】:Hashmap not being stored into a file?Hashmap没有被存储到文件中?
【发布时间】:2014-03-03 15:50:23
【问题描述】:

当我调试下面的代码时,它会在properties.store(fos, "none") 线上抛出一个FileNotFoundException,有什么具体原因吗?

编辑:堆栈跟踪是指以下内容:

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

}

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?
  • FileOutputStream constructor 会因各种原因抛出该异常。
  • 如何获得完整的堆栈跟踪?代码没有到达 catch 块。

标签: java eclipse file-io


【解决方案1】:

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

【讨论】:

  • 使用Java文件方法我确认用户有读写权限。
  • 您能否确认您的目录中是否创建了一个名为“Variables.ser”的文件?当您执行fos=new FileOutputStream(file); 时,它将创建文件。
  • 是的,我可以确认。
猜你喜欢
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 2013-04-28
  • 1970-01-01
相关资源
最近更新 更多