简体   繁体   中英

Adding a list of words to a txt file Java

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??

使用另一个提供'append'参数的FileWriter构造函数:

FileWriter aFileWriter = new FileWriter("mydata.txt", true );

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM