0

Im trying to add a list of words taken off the end user Via a JOptionPane input menu and store them in a text file without overwriting whats already there. eg of the txt file

bell cool hello now java compile

The problem I have is that it keeps overwriting what I have Any help??

  import javax.swing.JDialog;
  import java.util.Arrays;
  import javax.swing.*;
import java.util.*;
import java.io.*;

public class write
{
public static void main(String [] args) throws IOException
{
    PrintWriter out = new PrintWriter(aFileWriter);
 String word = JOptionPane.showInputDialog(null, "Enter a word");
  out.print(word);



  out.close();
  aFileWriter.close();
 }
 }

ok so now its appending the file but not moving to a new line for a new word??

user2205055
  • 97
  • 1
  • 5
  • 12

2 Answers2

2

Use another constructor for FileWriter that provides the 'append' argument:

FileWriter aFileWriter = new FileWriter("mydata.txt", true );
Andy Thomas
  • 82,182
  • 10
  • 99
  • 146
0

Open the file:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));

Append the string to the file:

out.println("the text");

Close file:

out.close();

Better explanation.

Community
  • 1
  • 1
gkiko
  • 2,273
  • 3
  • 30
  • 48