Hello. Wrote code to save the text that the user writes in the text field JTextArea . When you click the Restore button, the text that was saved, but this does not happen, should be displayed. Moreover, in the Saved.ser file, the text itself is not displayed as abracadabra instead. Tell me what to do?

 import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class TestSer implements Serializable { JTextArea text; public static void main(String[] args) { TestSer testSer = new TestSer(); testSer.go(); } public void go() { int width = 10; int height = 20; JFrame frame = new JFrame("Save me"); //Графическая часть JPanel panel = new JPanel(); text = new JTextArea(width, height); JButton buttonSave = new JButton("Save"); JButton buttonRestore = new JButton("Restore"); Box buttonBox = new Box(BoxLayout.Y_AXIS); frame.setSize(500,500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.NORTH, panel); frame.getContentPane().add(BorderLayout.SOUTH, buttonSave); panel.add(BorderLayout.NORTH, buttonBox); panel.add(text); buttonSave.addActionListener(new Saver()); buttonSave.addActionListener(new Restore()); buttonBox.add(buttonRestore); buttonBox.add(buttonSave); } public class Saver implements ActionListener { // Сохранение public void actionPerformed(ActionEvent a) { try { FileOutputStream fs = new FileOutputStream("Saved.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(text); os.close(); } catch(Exception ex) {ex.printStackTrace();} } } public class Restore implements ActionListener { // Загрузка public void actionPerformed(ActionEvent a) { try { FileInputStream fileStream = new FileInputStream("Saved.ser"); ObjectInputStream os = new ObjectInputStream(fileStream); Object one = os.readObject(); JTextArea text = (JTextArea) one; os.close(); } catch(Exception ex) {ex.printStackTrace();} } } } 

    2 answers 2

    When saving, here in this line:

     os.writeObject(text); 

    You save not a text at all, but an object of type JTextArea (since your text variable has type JTextArea ).

    To save text that is contained in a JTextArea , you must first obtain this text (using the getText() method of the getText() object).

    The FileInputStream and FileOutputStream are used to manipulate raw binary data. For text input / output, it is better to use FileReader and FileWriter respectively.

    Thus, saving can be implemented as follows:

     public class Saver implements ActionListener { public void actionPerformed(ActionEvent a) { try { FileWriter fileWriter = new FileWriter("Saved.ser"); fileWriter.write(text.getText()); fileWriter.close(); } catch(Exception ex) {ex.printStackTrace();} } } 

    And download like this:

     public class Restore implements ActionListener { public void actionPerformed(ActionEvent a) { try { BufferedReader bufferedReader = new BufferedReader(new FileReader("Saved.ser")); StringBuffer stringBuffer = new StringBuffer(); String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { stringBuffer.append(currentLine); } bufferedReader.close(); text.setText(stringBuffer.toString()); } catch(Exception ex) {ex.printStackTrace();} } } 

      Here you need to understand the difference between an object of type JTextArea (This is a graphical component, part of the interface, with its own methods and a bunch of data, in addition to the actual displayed text) and the actual text that is displayed in it. You, apparently, want to save not the graphic element, but the text entered into it - otherwise the meaning of your idea is completely incomprehensible. To save the actual text you need

      1. get it from JTextArea (using, for example, the getText() method);
      2. Write it to a plain text file (using, for example, the PrintStream class).

      You write to the file a complex object (a component of the GUI) with all its giblets that you absolutely do not need - this is the abracadabra that you see in the file.

      Next, to restore the text, you need to read the saved text from the file (using, for example, the FileReader class) and display it in your JTextArea . This can be done using the setText () method.

      Instead, for some reason you create a new local object: JTextArea text = (JTextArea) one; which is not used anywhere else. This is not the JTextArea text that you described at the beginning of the program, this is a new local variable. Read about scopes and local variables. And generally about classes - understand, how the String differs from JTextArea.