Opening, Editing and Saving text in JTextArea to .txt file

感情迁移 提交于 2020-01-15 12:15:27

问题


I am messing around with java swing and am trying to open a text file containing existing data with a JTextArea. It doesn't seem to be saving any changes regardless of the different things I have tried.

Below is code that reads the text file fine, but doesn't write it (obviously).

If someone could please advise me as to how I could successfully save changes to the JTextArea I would be greatful.

package funwithswing;


import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AllDataGUI extends JFrame{   

    public AllDataGUI(){
        fileRead();
        panels();         
    }

    private String storeAllString="";
    private JButton saveCloseBtn = new JButton("Save Changes and Close");
    private JButton closeButton = new JButton("Exit Without Saving");
    private JFrame frame=new JFrame("Viewing All Program Details");
    private JTextArea textArea = new JTextArea(storeAllString,0,70);


    private JButton getCloseButton(){
        return closeButton;
     }     

    private void fileRead(){
        try{    
          FileReader read = new FileReader("CompleteData.txt");
          Scanner scan = new Scanner(read);
             while(scan.hasNextLine()){
              String temp=scan.nextLine()+"\n";
              storeAllString=storeAllString+temp;
             }
      }
        catch (Exception exception)
         {
         exception.printStackTrace();
         }
    }      

    private void fileWrite(){
        try{
            FileWriter write = new FileWriter ("CompleteData.txt");
            textArea.write(write);
        }
        catch (Exception e){
            e.printStackTrace();
        }         
    }

    private void panels(){        
         JPanel panel = new JPanel(new GridLayout(1,1));
         panel.setBorder(new EmptyBorder(5, 5, 5, 5));
         JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
         rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
         JTextArea textArea = new JTextArea(storeAllString,0,70);
          JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          panel.add(scrollBarForTextArea); 
          frame.add(panel);
         frame.getContentPane().add(rightPanel,BorderLayout.EAST);
         rightPanel.add(saveCloseBtn);
          rightPanel.add(closeButton);
          closeButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e)
                    {
                        frame.dispose();

                    }
        });
           frame.setSize(1000, 700);
           frame.setVisible(true);   
           frame.setLocationRelativeTo(null);

}

    private void saveBtn(){

  }
}

回答1:


There are some mistakes in your code, I have modified those. Compile and run the below code This will solv your problem.

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AllDataGUI extends JFrame{   

    public AllDataGUI(){
        fileRead();
        panels();         
    }

    private String storeAllString="";
    private JButton saveCloseBtn = new JButton("Save Changes and Close");
    private JButton closeButton = new JButton("Exit Without Saving");
    private JFrame frame=new JFrame("Viewing All Program Details");
   // private JTextArea textArea = new JTextArea(storeAllString,0,70);
    private JTextArea textArea = new JTextArea();


    private JButton getCloseButton(){
        return closeButton;
     }     

    private void fileRead(){
        try{    
          FileReader read = new FileReader("CompleteData.txt");
          Scanner scan = new Scanner(read);
             while(scan.hasNextLine()){
              String temp=scan.nextLine()+"\n";
              storeAllString=storeAllString+temp;
             }
             textArea.setText(storeAllString);
      }
        catch (Exception exception)
         {
         exception.printStackTrace();
         }
    }      



    private void panels(){        
         JPanel panel = new JPanel(new GridLayout(1,1));
         panel.setBorder(new EmptyBorder(5, 5, 5, 5));
         JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
         rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
        // JTextArea textArea = new JTextArea(storeAllString,0,70);
          JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          panel.add(scrollBarForTextArea); 
          frame.add(panel);
         frame.getContentPane().add(rightPanel,BorderLayout.EAST);
         rightPanel.add(saveCloseBtn);
          rightPanel.add(closeButton);
          closeButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e)
                    {
                        frame.dispose();

                    }
        });
          saveCloseBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                saveBtn();
                frame.dispose();

            }
        });
           frame.setSize(1000, 700);
           frame.setVisible(true);   
           frame.setLocationRelativeTo(null);

}

    private void saveBtn(){
        File file = null;
        FileWriter out=null;


        try {
            file = new File("CompleteData.txt");
            out = new FileWriter(file);     
            out.write(textArea.getText());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  }
    public static void main(String[] arg)
    {
        new AllDataGUI();
    }
}



回答2:


You need to close FileWriter. Use try-with-resource of Java-7 or finally block to close resource properly.

private void fileWrite(){
    FileWriter write=null;
    try{
        write = new FileWriter ("CompleteData.txt");
        textArea.write(write);           
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally{
      if(write != null) 
       write.close();
    } 
}


来源:https://stackoverflow.com/questions/24076612/opening-editing-and-saving-text-in-jtextarea-to-txt-file

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