简体   繁体   English

无法在目录中创建文件 - Java

[英]Can't create file in directory - Java

I want to save all elements of a HashMap in a file.我想将HashMap的所有元素保存在一个文件中。 To do this I wrote following code with the help of some google searches:为此,我在一些谷歌搜索的帮助下编写了以下代码:

 public void saveCalendars() {
        
        try {FileOutputStream fos = new FileOutputStream(CALENDARPATH_STRING);
                 ObjectOutputStream oos = new ObjectOutputStream(fos);
                for(Calendar elementCalendar : calendarRegister.values()) {
                    oos.writeObject(elementCalendar);
                }
                 oos.close();
                 fos.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            try {FileOutputStream fos = new FileOutputStream(new File(CALENDARPATH_STRING));
                     ObjectOutputStream oos = new ObjectOutputStream(fos);
                    for(Calendar elementCalendar : calendarRegister.values()) {
                        oos.writeObject(elementCalendar);
                    }
                     oos.close();
                     fos.close();
            } catch (IOException ex) {
                System.out.println("Creating: Error initializing stream");
                }
        } catch (IOException e) {
            System.out.println("Save: Error initializing stream");
            }

    }

With final static String CALENDARPATH_STRING = "C:\\Windows\\calendars.dat"; final static String CALENDARPATH_STRING = "C:\\Windows\\calendars.dat"; . . I thought that I simply could use the same Code but with FileOutputStream fos = new FileOutputStream(new File(CALENDARPATH_STRING));我以为我可以简单地使用相同的代码,但使用FileOutputStream fos = new FileOutputStream(new File(CALENDARPATH_STRING)); if the file hasn't been created yet to create one.如果文件尚未创建,则创建一个。 Unfortunately, it doesn't work.不幸的是,它不起作用。 It's the firs time, that a make such saving stuff, so maybe you can help me.这是第一次做这么省钱的东西,所以也许你可以帮助我。

A couple of suggestions:几个建议:

  1. Use File.createNewFile to create a new file and verify it's result使用File.createNewFile创建一个新文件并验证它的结果
  2. Use try-with-resources when dealing with IO stuff (I assume you use > JDK 7).在处理 IO 的东西时使用try-with-resources (我假设你使用 > JDK 7)。 You can read more about this feature on official site .您可以在官方网站上阅读有关此功能的更多信息。

You can avoid duplications:您可以避免重复:

    File calendarFile = new File(CALENDARPATH_STRING);
    try {
        if(calendarFile.createNewFile()) {
            System.out.println("File not found. New file was created");
        }
    } catch (IOException e) {
        System.out.printf("Can not create file %s\n", CALENDARPATH_STRING);
    }

    try(FileOutputStream fos = new FileOutputStream(calendarFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos)) {
        for(Calendar elementCalendar : calendarRegister.values()) {
            oos.writeObject(elementCalendar);
        }
    } catch (IOException e) {
        System.out.println("Save: Error initializing stream");
    }

Well, I can see that there is an issue with the path of your file CALENDAR_PATH_STRING好吧,我可以看到您的文件CALENDAR_PATH_STRING的路径有问题

and use new File(CALENDAR_PATH_STRING) it would create a new file if the particular file was not found.并使用new File(CALENDAR_PATH_STRING)如果找不到特定文件,它将创建一个新文件。 Also in local I can see it is working.同样在本地,我可以看到它正在工作。

   public void saveCalendars(Map<String, Calendar> calendarRegister) {

     try (FileOutputStream fos = new FileOutputStream(new File(CALENDAR_PATH_STRING)); 
         ObjectOutputStream oos = new ObjectOutputStream(fos)) {
        for (Calendar elementCalendar : calendarRegister.values()) {
            oos.writeObject(elementCalendar);
        }
      } catch (IOException e) {
        System.out.println("Creating: Error initializing stream");
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM