1

I'm using Apache libraries to edit DOCX file and I want user to choose dir where to save his file. It doesnt matter what folder to select it always thows an excetion and says "path (Access denied)", however, if I choose the directory in my code it works perfectly. Here's some of my code:

        XWPFDocument doc = null;
        try {
            doc = new XWPFDocument(new ByteArrayInputStream(byteData));
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* editing docx file somehow (a lot of useless code) */

        Alert alert = new Alert(Alert.AlertType.INFORMATION);

        DirectoryChooser dirChooser = new DirectoryChooser();
        dirChooser.setTitle("Choose folder");
        Stage stage = (Stage) (((Node) event.getSource()).getScene().getWindow());
        File file = dirChooser.showDialog(stage);
        if (file != null) {
            try {
                doc.write(new FileOutputStream(file.getAbsoluteFile()));
                alert.setContentText("Saved to folder " +  file.getAbsolutePath());
            } catch (IOException e) {
                alert.setContentText(e.getLocalizedMessage());
            }
        } else {
            try {
                doc.write(new FileOutputStream("C://output.docx"));
                alert.setContentText("Saved to folder C:\\");
            } catch (IOException e) {
                alert.setContentText(e.getLocalizedMessage());
            }
        }
        alert.showAndWait();

Please help me to figure out what I'm doing wrong :(

Vasyl Butov
  • 454
  • 1
  • 4
  • 19
  • You are choosing a directory to save the file and you are trying to override that directory with your file. Instead just save the file like `doc.write(new FileOutputStream(file.getAbsoluteFile()+"\\EditedDoc.docx"));` – JKostikiadis Jun 14 '18 at 20:26
  • Also in future try to provide the error message in your answer cause it helps a lot. Finally as an advice try to use `System.out.println()` in order to debug your code, for example print the `file.getAbsoluteFile()` and check what you will get. – JKostikiadis Jun 14 '18 at 20:29
  • @JKostikiadis thanks a lot! Coding for 8 hrs makes me so blind. Thanks for advice as well. Have a good day, sir – Vasyl Butov Jun 14 '18 at 20:33

1 Answers1

2

DirectoryChooser returns a File object which is either a directory or a null (if you did not choose one by pressing cancel or exit the dialog). So in order to save your file, you need to also append the file name to the absolute path of the directory you choose. You can do that by :

doc.write(new FileOutputStream(file.getAbsoluteFile()+"\\doc.docx"));

But this is platform dependent cause for windows it’s ‘\’ and for unix it’s ‘/’ so better use File.separator like :

doc.write(new FileOutputStream(file.getAbsoluteFile()+File.separator+"doc.docx"));

You can read more about the above here

Edit: As Fabian mentioned in the comments below you can use the File constructor, passing the folder ( the file you got from them DirectoryChooser ) and the new file name as parameters which makes the code far more readable :

new FileOutputStream(new File(file, "doc.docx"))
JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
  • 1
    I recommend using the file constructor to get rid of the seperator: `new FileOutputStream(new File(file, "doc.docx"))` – fabian Jun 14 '18 at 21:02
  • @fabian indeed it is better and makes the code more readable like that, I will edit my answer tomorrow cause I cant right now. – JKostikiadis Jun 14 '18 at 21:13