0
votes

I'm trying to save text from area into a new file with JavaFX.

The user has previously uploaded the file and the text later on gets printed into the textarea.

So far all I've gotten with my saveButton is this:

         btnSave.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            FileChooser saveFile = new FileChooser();
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
            saveFile.getExtensionFilters().add(extFilter);
            File f = saveFile.showSaveDialog(primaryStage);

        }
    });

Thanks!

1
First things first, an extension does not define the format of a file at all. You can have a text file named "foo.exe". This only serves to filter entries displayed in the FileChooser. - fge
Edit: Got the method working. But I am curious as to how I am supposed to choose directory when doing this aproach. Or is this simply to rewrite the old file with the new String? - Johan Rovala

1 Answers

0
votes

You can try this:

    ObservableList<CharSequence> paragraph = textArea.getParagraphs();
    Iterator<CharSequence>  iter = paragraph.iterator();
        BufferedWriter bf = new BufferedWriter(new FileWriter(new File("textArea.txt")));
        while(iter.hasNext())
        {
            CharSequence seq = iter.next();
            bf.append(seq);
            bf.newLine();
        }
        bf.flush();
        bf.close();