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

解决方案 »

  1.   

    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解决
      

  2.   

    假设anguage.txt文件中帐号和密码是使用一个空格来分隔的,从文件中读出帐号、密码并打印出来的程序如下:
    public static void main(String[] args)
    {
    File file = new File("d:/anguage.txt");
    if (file.exists()==false){
    System.out.println("cannot find file");
    return;
    }
    try
    {
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String lineText;
    lineText = br.readLine();
    while(lineText != null){
    printUserAndPwd(lineText);
    lineText = br.readLine();
    }
    } catch ( FileNotFoundException e )
    {
    e.printStackTrace();
    } catch ( IOException e )
    {
    e.printStackTrace();
    }
    }

    public static void printUserAndPwd(String text){
    if (text == null)
    return;
    if ("".equals(text))
    return;

    String[] idAndPwd = text.split(" ");
    if (idAndPwd == null || idAndPwd.length != 2)
    {
    System.out.println("文本格式错误");
    return;
    }
    else{
    System.out.print("UserId = " + idAndPwd[0]);
    System.out.print("\t");
    System.out.println("Password = " + idAndPwd[1]);
    }
    }
      

  3.   

    用FileInputStream把文件读入到一个String内 再调用String的split函数 然后用循环把user和password push到hashmap里面 要哪个读哪个
      

  4.   

    BufferedReader 里的readLine()就可以实现文件的逐行读取了
      

  5.   

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    public class ReadTxt1 {
    public static void main(String[] args) {
    try {
    FileInputStream fis = new FileInputStream("D:\\sql.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String s;
    StringBuffer sb = new StringBuffer();
    while((s = br.readLine())!=null){
    sb.append(s + "\n");
    }
    System.out.println(sb.toString());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  6.   

    这个类java.util.Scanner有没有用过,java基础还有待提高那,呵呵,可惜分太少
      

  7.   

    最好是用户名和密码能用符号隔开 然后直接IO读取TXT
      

  8.   

    1楼的方法不错,而且有时你也可以用regular expressions来做,
    当然把文件是.xml格式的,dom4j会非常方便的
      

  9.   

    怎么还没有揭帖,我给代码,搂主给分吧;
    public class ReadWithScanner
    {
    // PRIVATE //
    private static File fFile;

    //private boolean breakFlag = false; public ReadWithScanner(String aFileName)
    {
    fFile = new File(aFileName);
    } public final void processLineByLine() throws FileNotFoundException
    {
    Scanner scanner = new Scanner(fFile);
    if(fFile.exists())
    {
    log("exsit file");
    }
    try
    {
    while (scanner.hasNextLine())
    {
    processLine(scanner.nextLine());
    }
    } finally
    {
    // ensure the underlying stream is always closed
    scanner.close();
    }
    }
    protected void processLine(String aLine)
    {
    // use a second Scanner to parse the content of each line
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if(scanner.hasNext())
    {
    log("sadfasfasfasdfa");
    try
    {
    String name = scanner.next();
    String value = scanner.next();
    log("Name is : " + quote(name.trim()) + ", and Value is : "
    + quote(value.trim())); }
    catch(NoSuchElementException nsee)
    {
    log("This line contains not this sign");
    }
    }
    // else
    // {
    // log("Empty or invalid line. Unable to process.");
    // }
    // (no need for finally here, since String is source)
    scanner.close();
    }
    private static void log(Object aObject)
    {
    System.out.println(String.valueOf(aObject));
    }
    public static void main(String... aArgs) 
    {
    ReadWithScanner parser = new ReadWithScanner("C:\\test\\test.txt");

    try
    {
    FileUtil.getFileNumber(fFile);

    catch (UtilException e1)
    { e1.printStackTrace();
    }
    try
    {
    parser.processLineByLine();

    catch (FileNotFoundException e)
    {
    e.printStackTrace();
    }
    log("Done.");
    }}
      

  10.   

    靠 那么大数据用txt
    杂不用xml 
    那个多好啊
    你想怎么搞就怎么搞 
    何乐而不讳啊