简体   繁体   中英

Can't create file in directory - Java

I want to save all elements of a HashMap in a file. 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"; . I thought that I simply could use the same Code but with 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
  2. Use try-with-resources when dealing with IO stuff (I assume you use > 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

and use new File(CALENDAR_PATH_STRING) it would create a new file if the particular file was not found. 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");
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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