我编写了有关登录系统的代码。它具有用户和管理员。关于用户,他们可以更改密码。用户帐户已经在txt文件中,并且当用户更改密码时,必须将新密码写入该txt文件中,并将其他用户保留在txt文件中。问题是,当我单击“更改密码”时,使用用户名写一个新密码,但在txt文件中仍然具有要更改密码的用户的旧密码。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
public class TEST4
{
public static Scanner S;
public static Scanner H;
private JFrame Frame;
private JPanel Panel;
private JButton Button;
private JButton Button1;
private JLabel Label1;
private JLabel Label;
private JLabel Label3;
private JLabel Label4;
JTextField txt = new JTextField("",50);
JTextField txt1 = new JTextField("",50);
public static void main(String[] args)
{
System.out.println("Loading...");
new TEST4 ();
}
public TEST4 ()
{
Application();
}
public void Application()
{
Frame = new JFrame("Welcome");
Frame.setVisible(true);
Frame.setSize(400,400);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel = new JPanel();
//Panel.setBackground(Color.BLACK);
Panel.setLayout(null);//Override auto arrange elements
txt1.setBounds(80, 50, 100, 25);
txt.setBounds(80, 10, 100, 25);
Button = new JButton("Sign in");
Button.setBounds(80, 90, 100, 25);//(x,y,w,h)
Label = new JLabel("username : ");
Label.setBounds(10, 10, 100, 25);
Label1 = new JLabel("password : ");
Label1.setBounds(10, 50, 100, 25);
Panel.add(Button);
Panel.add(Label);
Panel.add(Label1);
Panel.add(txt);
Panel.add(txt1);
Frame.add(Panel);
Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String X=txt.getText();
String X1=txt1.getText();
String filepath="username.txt";
Search(X,X1,filepath);
Frame.setVisible(false);
JFrame Frame2 = new JFrame("User Setting");
Frame2.setVisible(true);
Frame2.setSize(400,400);
Frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel2 = new JPanel();
Panel2.setLayout(null);
Label3= new JLabel("Welcome user");
Label3.setBounds(5, 10, 100, 25);
JTextField txt3 = new JTextField("",50);
txt3.setBounds(80,50,100,25);
Button1= new JButton("Change password");
Button1.setBounds(180, 60, 100, 25);
Button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent b)
{
String X2=txt3.getText();
String user="";
String pass="";
boolean found=false;
try
{
H= new Scanner(new File(filepath));
H.useDelimiter("[,\n]");
while (H.hasNext() && !found)
{
user=H.next();
pass=H.next();
FileWriter H=new FileWriter("username.txt",true);
H.close();
if(user.trim().equals(X.trim()) && pass.trim().equals(X1.trim()))
{
FileWriter J = new FileWriter("username.txt",true);
J.write("\r\n");
J.write(X+","+X2);
J.write("\r\n");
J.close();
}
}
H.close();
}
catch(Exception h)
{
System.out .print("error");
}
}
});
Panel2.add(Label3);
Panel2.add(txt3);
Panel2.add(Button1);
Frame2.add(Panel2);
if(X.equals("tan123") && X1.equals("123456"))
{
Frame.setVisible(false);
Frame2.setVisible(false);
JFrame Frame3= new JFrame("Admin setting");
Frame3.setVisible(true);
Frame3.setSize(400,400);
Frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel3 = new JPanel();
Panel3.setLayout(null);
Label4= new JLabel("Welcome admin");
Label4.setBounds(5, 10, 100, 25);
JTextField txt4 = new JTextField("",50);
txt4.setBounds(80,50,100,25);
Panel3.add(Label4);
Panel3.add(txt4);
Frame3.add(Panel3);
}
}
});
}
public static void Search(String X,String X1,String filepath)
{
boolean found=false;
String username="";
String password="";
try
{
S=new Scanner (new File(filepath));
S.useDelimiter("[,\n]");
while (S.hasNext() && !found)
{
username=S.next();
password=S.next();
if(username.trim().equals(X.trim()) && password.trim().equals(X1.trim()))
{
found=true;
}
}
S.close();
if(found==true)
{
System.out.print("welcome");
}
else
{
System.out.print("invalid");
}
}
catch(Exception a)
{
System.out.println("error");
}
}
}
为此一个新人,所以还是一团糟。
这是具有旧用户名和密码的旧txt文件
tan,123
shay,098
当我将用户tan的密码更改为456时,就这样
tan,123
shay,098tan,456
如何删除tan,123
并将其替换为tan,456
并仍然保留shay,098
。
对不起,我的英语不好,谢谢!!!