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

snz8szmq  于 2022-11-04  发布在  Eclipse
关注(0)|答案(2)|浏览(233)

我正在写一个Java程序,它是一个密码验证器。用户输入一个密码,然后要求通过再次输入来确认密码。如果第二个密码与第一个密码匹配,则密码将被确认并设置为密码变量。但如果密码不匹配,则输入的第二个密码将被记录并记录在一个预先存在的txt文件中。此外,循环将再次开始,并要求用户重新输入密码并确认。
我试过创建一个文件,然后在else if条件下写入它,然而,这意味着每次用户输入错误的密码时,都会再次创建txt文件,并且只记录最后输入的密码,而不是输入的错误密码的整个列表。
下面是我的代码。

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

            }

        }
    }

谢谢你花时间和我一起看这个。

nhn9ugyo

nhn9ugyo1#

你的程序运行得很好。唯一的变化是这个。
您应该使用此构造函数。只需添加true作为最后一个参数

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

这是描述

/**
 * 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));
}
4nkexdtk

4nkexdtk2#

**public FileWriter(字符串文件名,布尔值追加)**应该可以为您工作。

相关问题