Save JTextArea text to a txt file

て烟熏妆下的殇ゞ 提交于 2019-12-05 14:48:26

I think the problem is that you create an instance of SaveClass in the Main class, but in the Save method, that is in the SaveClass you create another instance, and you read the text from that instance. So you might want to do this to the Save() method:

delete the SaveClass sa = new SaveClass(); 

and then:

String text = this.getText1();
        FileWriter pw = new FileWriter ("filename.txt");
        txtarea.write(pw); //Object of JTextArea

You need only two statements to write content of JTextArea into file...

I hope this will help you...

It seems to me that you are never adding your JTextArea to the JFrame. More specifically, you add your JTextArea called textarea1 to a JPanel called card1 but this JPanel is never added to the JFrame.

EDIT: Oh I see that you are adding the SaveClass, which is a JPanel to the frame (although there are many, many JPanels that seemingly have nothing in them in this SaveClass). However, when you go to call the Save() method, you make a brand new SaveClass. In this new SaveClass the JTextArea is initialized with nothing in it to it doesn't write anything to the file.

You probably need an out.flush() just before the out.close().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!