有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组JAVA登录页面;如何使用阅读记事本两次?

我要求我的JAVA程序,我正在处理登录页面。在我的密码里。我有一系列读取方法来获取数组列表。然后在注册按钮,一旦我已经输入了一个数据,顺便说一句,我使用了bufferedWriter,所以它将附加到我现有的。txt文件

然后是我的问题,数组列表尚未更新,因为数组中的数据仅在启动中,这意味着仅从程序开始,如果我已经有了新的输入,它将在我的登录方法中变为无效凭据。。我试着用注册按钮方法和私人课堂方法复制粘贴我的启动,但仍然不起作用,请帮忙,谢谢

以下是我的扫描仪方法代码:

private void initialize() throws FileNotFoundException {

    String datas = "";
    Scanner notePad = new Scanner(new File("loginData.txt"));
    List<String> temps = new ArrayList<String>();

    while (notePad.hasNext()) 
    {
      datas = notePad.next();
      temps.add(datas);
    }
    notePad.close();
    String[] dataFile = temps.toArray(new String[0]);
    int mainCount = dataFile.length-1;

框架代码如下,包括我的按钮。跳到注册按钮方法,下面是代码

JButton registerButton = new JButton("REGISTER");
    registerButton.setFont(new Font("Arial", Font.BOLD, 12));
    registerButton.addActionListener(new ActionListener() {
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e) 
        {
            int countRegister = 0;

            while (!registerEmail.getText().equalsIgnoreCase(dataFile[countRegister]) && mainCount >= countRegister)
                {
                countRegister = countRegister + 2;
                    if (mainCount < countRegister) 
                    {
                    break;
                    }
                }

            if (mainCount <= countRegister) // No Existing Account
            {   
            try {
                FileWriter writer = new FileWriter("loginData.txt", true);
                BufferedWriter bufferedWriter = new BufferedWriter(writer);
                bufferedWriter.newLine();
                bufferedWriter.write(registerEmail.getText());
                bufferedWriter.newLine();
                bufferedWriter.write(registerPassword.getText());
                bufferedWriter.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            JOptionPane.showMessageDialog(frame, "Registered successfully, you can now login.", "Success", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
            JOptionPane.showMessageDialog(frame, "Sorry, you have entered an existing account.", "Error", JOptionPane.ERROR_MESSAGE);   // Existing Account
            }
        }
    });
    registerButton.setBounds(194, 502, 97, 23);
    frmImma.getContentPane().add(registerButton);

共 (1) 个答案

  1. # 1 楼答案

    在逻辑调用此方法获取刷新输入之前,可以创建一个方法来获取数据并进行比较

    private String[] data = new String[0];
    
    private void selectData() throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("loginData.txt"));
        List<String> temps = new ArrayList<String>();
        while (notePad.hasNextLine()) {
          temps.add(notePad.nextLine());
        }
        scanner.close();
        dataFile = new String[temps.size()];
        dataFile = arrlist.toArray(dataFile);
    }
    

    ,您可以从JDK8+使用NIO

    private void selectData() throws IOException{
       dataFile = Files.lines(Paths.get(new 
       File("C:\\data.txt").getPath())).toArray(String[]::new);
    }