-4

I want the String text to be saved as a .txt file wherever I want. I think you can do that by using JFileChooser. Anyway here is my Code:

class TextEditorFrame extends JFrame {

    JFileChooser chooser;

    public TextEditorFrame(){

        Scanner s = new Scanner(System.in);
        chooser = new JFileChooser();

        String text = s.nextLine();

        try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))){
            out.print(text);
            System.out.println("Text Saved:");
            System.out.println(text);
        } catch (FileNotFoundException ex) {
            System.out.println("Something went wrong...");
        }
    }
}

This is my code so far. Any way I can combine the JFileChooser with the String text?

NOTE: This is not the full code but everything you need to know is in here.

Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35
fihdi
  • 145
  • 1
  • 1
  • 12

1 Answers1

2

This makes the PrintStream go onto the selected file of the JFileChooser you have made.

File file = chooser.showSaveDialog();
PrintStream out = new PrintStream(new FileOutputStream(file));
Jaboyc
  • 567
  • 1
  • 8
  • 16