#How to create a file in a certain directory?

4 messages · Page 1 of 1 (latest)

rocky wolf
#

I have a folder called "Saves" in the same directory as the program that I want to put save files, but I only know how to make a save file in the same directory as the program. How could I modify this code such that the saves are created in "Saves"? Researching this wasn't yielding a straight answer so I came here.

public void save(Serializable data, String fileName)
    {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)))
        {
            oos.writeObject(data);
        }
        catch (IOException ex) 
        {
            System.out.println("Error occured while saving:");
            System.out.println(ex.getMessage());
        }
    }
dawn timberBOT
#

This post has been reserved for your question.

Hey @rocky wolf! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rocky wolf
#

also, this is where "Saves" is created and also where the save method is used

saveButton.addActionListener(e -> 
        {
            File saves = new File("Saves");
            if (saves.mkdirs())
            {
                System.out.println("ye");
            }
            else
            {
                System.out.println("nah");
            }
            for (int i = 0; i<dayList.size(); i++)
            {
                rm.save((Serializable) dayList.get(i).workoutDropdown.getSelectedItem(), "day"+i+"Workout");
                rm.save((Serializable) dayList.get(i).equipmentDropdown.getSelectedItem(), "day"+i+"Equipment");
                rm.save((Serializable) dayList.get(i).setsTField.getText(), "day"+i+"Sets");
                rm.save((Serializable) dayList.get(i).repsTField.getText(), "day"+i+"Reps");
            }
        });