简体   繁体   English

覆盖Java中的文本文件

[英]Overwriting a text file in Java

The application should be able to register a new user and save data to text file which is already has many users' data. 该应用程序应该能够注册新用户并将数据保存到已经具有许多用户数据的文本文件中。 This code is works but it doesn't save new data to text file. 此代码有效,但不会将新数据保存到文本文件。 How should I overwrite the text file? 我应该如何覆盖文本文件? Should I read data from text first and write all the data back to text file or there is a way to just add a new line and write new data there? 我应该先从文本中读取数据,然后将所有数据写回到文本文件中,还是可以添加一行并在其中写入新数据?

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");
            }
        }
    });

The text file format is like this 文本文件格式是这样的

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

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

FileReader does have this constructor FileReader确实具有此构造函数

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

the true will append the new data to the existing data true会将新数据追加到现有数据

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

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