Noah Cagle
Noah Cagle

Reputation: 146

Saving String with JFileChooser

and I am making a program in java just like notepad. So I have the saving down, and it works, here is the code for example

BufferedWriter writer = null;
                    try {
                        writer = new BufferedWriter(new FileWriter(link.getText()));
                        writer.write(display.getText());
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    } finally {
                        try {
                            if (null != writer) {
                                writer.close();
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }

Anyway, what this does is, you cant see it in the code, but it makes a JFrame that has a JTextField and a JButton that what you do is put the link in the JTextField (ex: C:\Users\Noah\Desktop\text.txt) and it saves when you press save. But I want to do this with a JFileChooser. Can someone help me?

EDIT:

Solution:

JFileChooser fc = new JFileChooser("C:\\Users");
fc.showSaveDialog(frame); // frame is the JFrame (window)
BufferedWriter writer = null;
                try {
                    writer = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath()));
                    writer.write(display.getText());
                } catch (Exception e1) {
                    e1.printStackTrace();
                } finally {
                    try {
                        if (null != writer) {
                            writer.close();
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

I figured this out way after i posted this

Upvotes: 0

Views: 605

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

You have a lot examples in Google, anyways here is some snippets of code where I save xml file:

    public void SaveMe(){

    //Configure fileChooser
    JFileChooser fc = new JFileChooser(lastOpenDir); // after 1st save store path to "lastOpenDir"

    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

    fc.setDialogTitle("Save only XML");


    fc.removeChoosableFileFilter(fc.getFileFilter());  //remove the default file filter

    FileFilter filter = new FileNameExtensionFilter("XML file", "xml");

    fc.addChoosableFileFilter(filter); //add XML file filter

    //show dialog
    int returnVal = fc.showSaveDialog(appFrame);

    if(returnVal == JFileChooser.APPROVE_OPTION){

        File selectedDir = fc.getSelectedFile();

        lastOpenDir=fc.getSelectedFile().getParent();

        lastOpenFile = fc.getSelectedFile().getName();

        if(selectedDir.isFile()){

            String errorString = selectedDir.getPath()+" already exists.\nDo you want to replace it?"; 

            Object[] options = {"Yes", "No"};

            int n = JOptionPane.
                    showOptionDialog(
                            null,
                            errorString,
                            "Override",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                            null,
                            options,
                            options[0]
                            );

            if (n == JOptionPane.YES_OPTION){

                if(fc.getFileFilter().getDescription().equals("XML file")){

                    // save selectedDir.getPath() 


                }
            } 

Upvotes: 1

Related Questions