-1

while i am trying to save the file its not working but for making folders it works. what should i do ? i am new in java also. plz help

public void actionPerformed(ActionEvent ae)
{   
    if(ae.getSource()==save)
    {
        JFrame parentFrame = new JFrame();

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Specify a file to save");    

        int userSelection = fileChooser.showSaveDialog(parentFrame);

        if (userSelection == JFileChooser.APPROVE_OPTION) 
        {
            File fileToSave = fileChooser.getSelectedFile();
            System.out.println("Save as file: " + fileToSave.getAbsolutePath());
        }   
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80

2 Answers2

2

You choose a file but don't create it write anything to it. The file will not be created until you actually create it or write something to it, for example with

FileWriter writer = new FileWriter(fileToSave);
writer.write("Hello!");
writer.close();
Joni
  • 108,737
  • 14
  • 143
  • 193
0

First, get the file you want to save as a File. Then, write it to a new directory using BufferedWriter to a new directory.

final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(aComponent); //parent component to JFileChooser
if (returnVal == JFileChooser.APPROVE_OPTION) { //OK button pressed by user
        File file = fc.getSelectedFile(); //get File selected by user
        o = new BufferedWriter(new FileWriter(file)); //use its name
        //write things here
        o.flush();
        o.close();
}

Look at How to save file using JFileChooser in Java? and How to save a txt file using JFileChooser?

Community
  • 1
  • 1
mkaminsky
  • 353
  • 2
  • 10