简体   繁体   English

如何将字符串中的文本添加到预先存在的 txt 文件中?

[英]How do I add text from a string to a pre-existing txt file?

I am writing a Java program that is a password authenticator.我正在编写一个密码验证器 Java 程序。 The user enters a password and then is asked to confirm the password by re-entering it.用户输入密码,然后要求通过重新输入密码来确认密码。 If the second password matches the first the password will be confirmed and set to the password variable.如果第二个密码与第一个密码匹配,则密码将被确认并设置为密码变量。 But if the passwords don't match then the second password entered should be recorded and logged in a pre-existing txt file.但如果密码不匹配,则应记录输入的第二个密码并将其记录在预先存在的 txt 文件中。 As well as this the loop will start again and the user will be asked to re-enter their passwords and confirm them.除此之外,循环将再次开始,用户将被要求重新输入密码并确认。

I have tried creating a file and then writing to it in the else if condition, however then that means the each time the user enters a wrong password the txt file is created again and only the last entered password is recorded instead of the entire list of wrong passwords that were entered.我尝试创建一个文件,然后在 else if 条件下写入它,但这意味着每次用户输入错误的密码时,都会再次创建 txt 文件,并且只记录最后输入的密码,而不是整个列表输入的密码错误。

Here is my code below.下面是我的代码。

package passwordAuthenticator.java;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Formatter;

public class passwordAuthenticator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String password;
        
        try {
              File myObj = new File("Passwords.txt");
              if (myObj.createNewFile()) {
                System.out.println("File created: " + myObj.getName());
              } else {
                System.out.println("File already exists.");
              }
            } catch (IOException e) {
              System.out.println("An error occurred.");
              e.printStackTrace();
            }
        

        while(true){
            System.out.println("Please enter password: ");
            Scanner sc = new Scanner(System.in);
            String firstPassword = sc.nextLine();
            
            System.out.println("Please confirm password: ");
            Scanner sc1 = new Scanner(System.in);
            String secondPassword = sc.nextLine();
            
                if(firstPassword.equals(secondPassword)) {
                    System.out.println("Password confirmed.");
                    password = secondPassword;
                    break;
                } else if(firstPassword != secondPassword) {
                    try {
                          FileWriter myWriter = new FileWriter("Passwords.txt");
                          myWriter.write(secondPassword);
                          myWriter.close();
                        } catch (IOException e) {
                          System.out.println("An error occurred.");
                          e.printStackTrace();
                        }
                    System.out.println("\nPasswords are not a match. Retry!\n");
                }
                    
            }
            
        }
    }

Thank you for your time looking at this with me.谢谢你花时间和我一起看这个。

Your programs works well.您的程序运行良好。 The only change is this.唯一的变化就是这个。
You should use this constructor.你应该使用这个构造函数。 Just add true as the last parameter只需添加true作为最后一个参数

FileWriter myWriter = new FileWriter("Passwords.txt", true);

Here's the description这是描述

/**
 * Constructs a FileWriter object given a file name with a boolean
 * indicating whether or not to append the data written.
 *
 * @param fileName  String The system-dependent filename.
 * @param append    boolean if <code>true</code>, then data will be written
 *                  to the end of the file rather than the beginning.
 * @throws IOException  if the named file exists but is a directory rather
 *                  than a regular file, does not exist but cannot be
 *                  created, or cannot be opened for any other reason
 */
public FileWriter(String fileName, boolean append) throws IOException {
    super(new FileOutputStream(fileName, append));
}

public FileWriter(String fileName, boolean append) should work for you. public FileWriter(String fileName, boolean append)应该适合你。

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

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