-2
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class two extends JFrame implements ActionListener
{


JLabel l1,l2;
    JTextArea a1,a2;
    JButton b1;
    JMenuBar bar;
    JMenu menu;
    JMenuItem m1,m2;
    JPanel p;
    ArrayList<String> a;
    DataOutputStream d1;

//gui part of the programme

public void go()
{
    p=new JPanel();
    bar=new JMenuBar();
    menu=new JMenu("File");
    m1=new JMenuItem("Save");
    m1.addActionListener(this);
    m2=new JMenuItem("New");
    menu.add(m1);
    menu.add(m2);


Font f=new Font("Arial",Font.BOLD,16);
l1=new JLabel("Question ");
l1.setAlignmentX(CENTER_ALIGNMENT);
l1.setFont(f);
l2=new JLabel("Answer");
l2.setFont(f);
l2.setAlignmentX(RIGHT_ALIGNMENT);
a1=new JTextArea(10,50);
JScrollPane a4 = new JScrollPane(a1);
a4.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
a4.setMaximumSize(new Dimension(600,220));
a4.setAlignmentX(CENTER_ALIGNMENT);
a2=new JTextArea(10,50);
JScrollPane a3 = new JScrollPane(a2);
a3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
a3.setMaximumSize(new Dimension(600,220));
a3.setAlignmentX(CENTER_ALIGNMENT);
b1=new JButton("Next Card");
b1.setAlignmentX(LEFT_ALIGNMENT);
b1.addActionListener(this);

bar.add(menu);
setJMenuBar(bar);
BoxLayout g=new BoxLayout(p,BoxLayout.Y_AXIS);
p.setLayout(g);
p.add(l1);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(a4);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(l2);
p.add(Box.createRigidArea(new Dimension(15,15)));
p.add(a3);
p.add(Box.createRigidArea(new Dimension(25,25)));
p.add(b1);
getContentPane().add(p);
setVisible(true);
setSize(500,600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

public static void main(String[] args) {
    two obj=new two();
    obj.go();
}


public void actionPerformed(ActionEvent e)
{
 a=new ArrayList<String>();

//part where the text area content is saved to a listarray

 if(e.getSource()==b1)
{
String l=new String(a1.getText()+"/");
String w=new String (a2.getText()+"\n");
a.add(l);
a.add(w);

//values are added to arraylist as shown by the value of a.size()

System.out.println(a.size());
a1.setText("");
a2.setText("");

}

//method to save listarray to a file using filechooser

if(e.getSource()==m1){

JFileChooser fileSave = new JFileChooser();
int retrival=fileSave.showSaveDialog(this);
File d=fileSave.getSelectedFile();

if (retrival == JFileChooser.APPROVE_OPTION) {

        try{
        System.out.println(d);

//this part is not being called

        d1=new DataOutputStream(new FileOutputStream(d+".txt");

        for(String o:a)
        {
        d1.writeChars(o);
     System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE!");
    }
        d1.close();


    }catch(Exception ex){System.out.println(ex);}
}
}

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

DataOutputStream is working as expected.

Nothing is written to file as a new ArrayList is created which replaces the previous instance a when actionPerformed is invoked.

a = new ArrayList<String>();

clearing the contents of any previous data so nothing is written to file. Move this statement to the constructor so that the ArrayList is only initialized once. Use clear to clear the List.

Aside: JTextArea has access to the write method which allows its content to be written directly to file

Reimeus
  • 158,255
  • 15
  • 216
  • 276