有个anguage.txt  里面记录着用户的帐号和密码   格式是:  aaa   111
         bbb   222
         ccc   333如何才能把里面的帐号和密码 分别读出  然后在用户登入框里里进行匹配验证,成功的话则登入成功,不成功的话则跳出登入失败~!  希望大家能帮帮忙 谢谢啦

解决方案 »

  1.   

    就是文件读取的操作。
    把读出的记录分离开后,保存在一个set中,就ok。
    另外,为什么不改用xml文件?或者数据库?
      

  2.   

    哎 题目要求就是要用txt的  
     但是怎么分段读txt 
    谁能提供我个代码啊~!
      

  3.   

    下面是一个简单的例子: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!");
      }
      }
    }
      

  4.   

    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数组每个元素存放一个用户(一行)
      

  5.   

    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>
      

  6.   

    我要设计一个登入框界面   界面上有两个Button “登入” 和“ 取消”
     还有个两个TextArea  用来输入用户帐号 和密码的 
        然后在某个地方有一个anguage.txt的文档  里面记录许多帐号和密码 
     
    txt的格式是:aaa   111
                 bbb   222
                 ccc   333
                 …… ……
    问如何把txt里的帐号和密码读取出来匹配验证  然后进行登入??
     
      谁能帮帮我下  提供完整的界面代码 感激不禁  谢 谢!~~~
      

  7.   

    我要设计一个登入框界面   界面上有两个Button “登入” 和“ 取消”
     还有个两个TextArea  用来输入用户帐号 和密码的 
        然后在某个地方有一个anguage.txt的文档  里面记录许多帐号和密码 
     
    txt的格式是:aaa   111
                 bbb   222
                 ccc   333
                 …… ……
    问如何把txt里的帐号和密码读取出来匹配验证  然后进行登入??
     
      谁能帮帮我下  提供完整的界面代码 感激不禁  谢 谢!~~~
    (是用JBuilder)
      

  8.   

    你的要求是 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();
    }
    }
    }}
      

  9.   

    不知道你文件的格式如何,不过读文件的内容很简单,
    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中保存的文件内容进行分析取出对应的属性就可以了
    ,剩下的就是纯字符串操作。多少给点分数吧,问问题把分问完拉!!!!!
      

  10.   

    不过好像就只能这么读吧,键/值对的那种txt,没找到方法