求助:Java 如何分行读取txt里内容进行登入身份验证~!
有个anguage.txt 里面记录着用户的帐号和密码
格式是: aaa 111
bbb 222
ccc 333
如何才能把里面的帐号和密码 分别读出 然后在用户登入框里里进行匹配验证,成功的话则登入成功,不成功的话则跳出登入失败~!
希望大家能帮帮忙 谢谢啦
[解决办法]
全部读下来分离字符串?
[解决办法]
就是文件读取的操作。
把读出的记录分离开后,保存在一个set中,就ok。
另外,为什么不改用xml文件?或者数据库?
[解决办法]
下面是一个简单的例子:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Login {
private BufferedReader reader;
public Login() {
init();
}
private void init() {
try {
reader = new BufferedReader(new FileReader(new File( "anguage.txt ")));
} catch (Exception e) {
}
}
public boolean checkUser(String username, String password) {
try {
String str = reader.readLine();
while(str != null) {
String[] info = str.split( " ");
System.out.println(info[0].trim() + " " + info[1].trim());
if(info[0].trim().equals(username) && info[1].trim().equals(password)) {
return true;
}
str = reader.readLine();
}
} catch (Exception e) {
System.out.println( "Error occured when verify user ");
}
return false;
}
public static void main(String[] args) {
if(args == null || args.length < 2) {
System.out.println( "Usage: java Login username password: \n e.g. java Login tony 657891 ");
return;
}
Login login = new Login();
if(login.checkUser(args[0], args[1])) {
System.out.println( "Congratulations! Login successfully ");
} else {
System.out.println( "Incorrect username or password! ");
}
}
}
[解决办法]
FileInputStream ss = new FileInputStream(fileUrl);
byte[] s = new byte[(int)ss.getChannel().size()];
ss.read(s);
String fileText = new String(s);
String[] users = fileText.split( "\r\n ");//users数组每个元素存放一个用户(一行)
[解决办法]
不用分段读,你全读出来放到字符串里面。看好格式,把你从输入框里得到用同样的格式看看再不在这个大字符串里不就行了
[解决办法]
Properties props = new Properties();
props.load(new FileInputStream( "filename.properties "));
String Id= props.getProperty( "userId ");
String Password= props.getProperty( "passWord ");
filename.properties格式为
userId=root;
password=111111;
如果LZ有多个用户的话,建议用DOM4J解决
try{
SAXReader reader = new SAXReader();
Document document = reader.read( "c:/Demo.txt ");
Element root = document.getRootElement();
for ( Iterator node = root.elementIterator(); node.hasNext(); ) {
// CompanyBean a=new CompanyBean();
Element element = (Element) node.next();
Attribute attribute = (Attribute) element.attribute( "value ");
a.setBirthday(element.elementText( "birthday "));
a.setDepartment((String)attribute.getData());
a.setName(element.elementText( "name "));
a.setSalary(element.elementText( "salary "));
System.out.println(a.getBirthday()+ "B "+a.getDepartment()+ "D "+a.getName()+ " N "+a.getSalary()+ " S ");
list.add(a);
}
}catch(Exception e){System.out.println(e.getMessage());}
c:/Demo.txt的格式为XML
<?xml version= "1.0 " encoding= "UTF-8 "?>
<UserList>
<User>
ID..
PASSWORD..
</User>
<User>
ID..
PASSWORD..
</User> </UserList>
[解决办法]
你的要求是 swing界面读取文本框的值吧。是的话,我做了一个。
a.txt格式:(将a.txt建立在此项目中)
admin 1234
aaa 123
ccc 456
ddd 5555
以下是 代码部分
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener {
private JTextField txtName = null;
private JPasswordField txtPwd = null;
private JLabel lblName = new JLabel( "姓名: ");
private JLabel lblPwd = new JLabel( "密码: ");
private JPanel pMain = new JPanel();
private JButton btnLogin = new JButton( "登陆 ");
private JButton btnReadUser = new JButton( "读取新用户 ");
private int click = 0;
private int row = 0;
private JLabel lblMsg = new JLabel();
FileInputStream fin = null;
public Login()
{
txtName = new JTextField(25);
txtPwd = new JPasswordField(25);
pMain.setLayout(new FlowLayout());
pMain.add(lblName);
pMain.add(txtName);
pMain.add(lblPwd);
pMain.add(txtPwd);
pMain.setBackground(Color.white);
pMain.add(btnLogin);
pMain.add(btnReadUser);
pMain.add(lblMsg);
//--------------------默认读取第一个用户
try {
fin = new FileInputStream( "a.txt ");
int flag = 0;
while (flag != -1)
{
flag = fin.read();
txtName.setText(txtName.getText()+(char)flag);
if(flag == 32)
{
break;
}
}
while(flag != -1)
{
flag = fin.read();
if(flag == 13)
{
break;
}
txtPwd.setText(txtPwd.getText()+(char)flag);
}
fin = new FileInputStream( "a.txt ");
while (flag != -1)
{
flag = fin.read();
if(flag == 13)
{
row ++ ;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//---------------------------
this.getContentPane().add(pMain);
this.setSize(350, 140);
this.setTitle( "登陆 ");
this.setVisible(true);
btnLogin.addActionListener(this);
btnReadUser.addActionListener(this);
}
public static void main(String[] args) {
new Login();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnReadUser))
{
click ++;
try {
if(click > row)
{
lblMsg.setText( "别乱点,哪来那么多用户 ");
return ;
}
fin = new FileInputStream( "a.txt ");
int flag = 0;
for(int i=0;i <click;i++)
{
while (flag != -1)
{
flag = fin.read();
if(flag == 10)
{
break;
}
}
}
txtName.setText( " ");
txtPwd.setText( " ");
while (flag != -1)
{
flag = fin.read();
txtName.setText(txtName.getText()+(char)flag);
if(flag == 32)
{
break;
}
}
while(flag != -1)
{
flag = fin.read();
if(flag == 13 || flag == -1)
{
break;
}
txtPwd.setText(txtPwd.getText()+(char)flag);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
[解决办法]
不知道你文件的格式如何,不过读文件的内容很简单,
BufferedReader bf=new BufferedReader(new FileInputStream(new File(file_dir)));
StringBuffer sb=new StringBuffer();
String content= " ";
while((content=bf.readLine())!=null){
sb.append(content);
}
System.out.println(sb.toString());
然后你可以对sb中保存的文件内容进行分析取出对应的属性就可以了
,剩下的就是纯字符串操作。
多少给点分数吧,问问题把分问完拉!!!!!