如何在不更改内容的情况下添加到文件(JAVA)?

时间:2016-03-29 18:24:45

标签: java file readfile

所以我正在创建一个基本的地址簿应用程序以获得乐趣。我希望用户能够单击(添加到书籍)按钮以保存输入到Address Book.txt文件中的当前信息。问题是我目前使用的代码擦除了所有文件内容,只添加了新条目。在不删除内容的情况下添加到文件中的好方法是什么,或者如果不可能存储所有文件数据并重新输入Address Book.txt文件?

这是我的代码

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

public class AddressBook extends JFrame{
//Create fields
    public ArrayList<String> data;
    public JTextField name,address,number;
    private JButton addStuff;
//Create the constructor for the Phone book 
    public AddressBook(){
        //set the layout
        setLayout(new FlowLayout());
        //Create instances of fields and give properties
        name = new JTextField("(Name)");
        name.setPreferredSize(new Dimension(200,20));
        address = new JTextField("(Address)");
        address.setPreferredSize(new Dimension(290,20));
        number = new JTextField("(Number)");
        number.setPreferredSize(new Dimension(100,20));
        //create a save button and add functionality 
        addStuff = new JButton("Add to Book");
        addStuff.addActionListener(
                new ActionListener(){



@Override
                    public void actionPerformed(ActionEvent e) {

我创建了一种访问和写入文件Address Book.txt的方法,但是当我这样做时它似乎正在删除所有数据

     PrintWriter writer;
                String x = String.format("Name: %s   Number: %s    Address: %s ", name.getText(),number.getText(),address.getText());
                String name = "Address Book.txt";
            try {
                writer = new PrintWriter(name, "UTF-8");
                writer.println(x);
//Go to new line to write info to
                    writer.println("");

                     writer.close();
                    }
                catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                        } 
                catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                        }

                    }
                }
            );
    add(name);
    add(address);
    add(number);
    add(addStuff);
}

我打算用它来读取文本文件中的所有内容并将其保存到String ArrayList(数据)中,但信息会在文件中打印出奇怪的信息。

private class ReadFile {

        private Scanner x;

        public void openFile(){
            try{
                x = new Scanner(new File("Address Book.txt"));
            }
            catch(Exception e)
            {
                System.out.println("File not found or something");
            }

        }
        public void readFile(){
            while(x.hasNext()){
                String a = x.next();
                data.add(a);
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

PrintWriter集创建FileInputStream以追加而不是截断:

writer = new PrintWriter(new FileOutputStream(name, true));

答案 1 :(得分:1)

而不是PrintWriter考虑使用设置为append的{​​{1}} FileWriter

true