Question

I'm playing around and I made a notepad-like app using swing. Everything is working properly so far, except it's not letting me save the text file directly on C:/. On any other disk, and INCLUDING the root of the D: drive, or in folders of the C:/ disk it works like a charm. Why is this happening?

This is my code:

   file_save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                JFileChooser Chooser = new JFileChooser();
                File DefaultDirectory = new File("C:/");
                File Path;
                int Checker;
                FileFilter text_filter = new FileNameExtensionFilter(
                        "Text File (*txt)", "txt");
                FileFilter another_filter = new FileNameExtensionFilter(
                        "Debug Filter (*boyan)", "boyan");
                //
                Chooser.setCurrentDirectory(DefaultDirectory);
                Chooser.setDialogTitle("Save a file");
                Chooser.addChoosableFileFilter(text_filter);
                Chooser.addChoosableFileFilter(another_filter);
                Chooser.setFileFilter(text_filter);
                Checker = Chooser.showSaveDialog(null);
                //
                if (Checker == JFileChooser.APPROVE_OPTION) {
                    Path = Chooser.getSelectedFile();
                    System.out.println(Path.getAbsolutePath());
                    ;// Just for
                        // debugging.

                    BufferedWriter writer = null;
                    try {
                        writer = new BufferedWriter(new FileWriter(Path
                                .getAbsolutePath()));
                        String[] myString = textArea.getText().split("\\n");
                        for (int i = 0; i < textArea.getLineCount(); i++) {
                            writer.append(myString[i]);
                            writer.newLine(); // SO IT CAN PRESERVE NEW LINES
                                                // (APPEND AND SPLIT ARE ALSO
                                                // THERE
                                                // BECAUSE OF THAT)
                            writer.flush();
                        }

                        JOptionPane.showMessageDialog(null, "File saved.", "",
                                JOptionPane.WARNING_MESSAGE);

                    } catch (IOException e) {
                        JOptionPane.showMessageDialog(null,
                                "File did not save successfuly.", "",
                                JOptionPane.WARNING_MESSAGE);
                    } finally {
                        try {
                            if (writer != null)
                                writer.close();
                        } catch (IOException e) {
                            JOptionPane.showMessageDialog(null,
                                    "File did not save successfuly.", "",
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    }

                }

            }
        });

Thanks a lot in advance!

Was it helpful?

Solution

Usually, one does not have write permissions in C:\.

  • Start the app as a privileged user

    One should not do that, as it is not intended by OS design. Changing permissions on C:\, or the system drive respectively, is a no-go.

  • Save into a sub-directory of System.getProperty("user.home"); (way to go)

    The user home could also be a network folder with nighly backup in a domain network, for example. Especially for remote sessions (RDP, Citrix), this is often the case.

If you absolutely need to install a static file outside of the users folders, do it once, with an installer, configured to raise privileges (UAC).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top