简体   繁体   English

将单词列表添加到txt文件Java

[英]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. 我试图通过JOptionPane输入菜单添加从最终用户处取出的单词列表,并将它们存储在文本文件中,而不会覆盖已存在的内容。 eg of the txt file 例如txt文件

bell cool hello now java compile 贝尔酷你好现在java编译

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 . 更好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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