用Java覆盖文本文件

时间:2014-08-28 18:02:20

标签: java text-files overwrite filewriter bufferedwriter

应用程序应该能够注册一个新用户并将数据保存到已经有很多用户的文本文件中。数据。此代码有效,但它不会将新数据保存到文本文件中。我该如何覆盖文本文件?我应该首先从文本中读取数据并将所有数据写回文本文件,还是可以添加新行并在那里写入新数据?

registerB.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            String getFullname = fullnameT.getText();
                            String getUsername = usernameT.getText();
                            String getPassword = passwordT.getText();
                            String getPN = pnT.getText();
                            String getEmail = emailT.getText();
                            boolean theSame = false;
                            String []Fullname = new String[20];
                            String []Username = new String[20];
                            String []Password = new String[20];
                            String []PN = new String[20];
                            String []Email = new String[20];

            if(!getFullname.isEmpty() && !getUsername.isEmpty() && !getPassword.isEmpty() && !getPN.isEmpty() && !getEmail.isEmpty()){
                int k = 0;
                try {
                    Scanner in = new Scanner (new FileReader("login.txt", true));
                    while (in.hasNext()) {
                        Fullname[k] = in.next();
                        Username[k] = in.next();
                        Password[k] = in.next();
                        PN[k] = in.next();
                        Email[k] = in.next();
                        in.next();

                        if(getUsername.equals(Username[k])){
                            JOptionPane.showMessageDialog(null, "Username already has been registered.");
                            theSame = true;
                            break;
                        }
                        k++;
                    }
                }catch(Exception ew){}
                try
                {
                    String path = "login.txt";

                    File file = new File(path);

                    FileWriter fileWriter = new FileWriter(file,true);

                    BufferedWriter bufferFileWriter  = new BufferedWriter(fileWriter);

                    fileWriter.append("," + getFullname + "," + getUsername + "," + getPassword + "," + getPN + "," + getEmail +"," + "\n");

                    bufferFileWriter.close();

                    System.out.println("User Registration Completed");

                }catch(Exception ex){
                    System.out.println(ex);
                }
            else{
                JOptionPane.showMessageDialog(null, "One or some of the fields is/are empty");
            }
        }
    });

文本文件格式是这样的

fullname,username,password,phonenumber,email fullname,username,password,phonenumber,email

1 个答案:

答案 0 :(得分:0)

这是错误的new FileReader("login.txt", true)

FileReader确实有这个构造函数

new FileWriter("login.text")更改为new FileWriter("login.text", true)

true会将新数据附加到现有数据