0

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!

Johan Rovala
  • 174
  • 1
  • 2
  • 14
  • 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 Feb 13 '15 at 18:06
  • possible duplicate of [How do I save a String to a text file using Java?](http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java) – James_D Feb 13 '15 at 18:14
  • 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 Feb 13 '15 at 18:20

1 Answers1

0

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();
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Fotic
  • 301
  • 5
  • 15