简体   繁体   中英

Java bufferedreader reading

I am attempting to make a different type of login system. I want to make it so that it reads off a .txt file, containing details such as this (format is username - password - boolean):

guest51 password true
guest52 bus true
guest53 password123 true
guest54 123password false

However, here is where it gets very difficult. I want to then check for the boolean, if it is "true", skip the account, if it isn't, use it. From there, when it's "finished" with the account, set it to "true".

I would want the usage to be something such as:

username = nextAvaialableAccount.getUsername();
password = nextAvailableAccount.getPassword();

I have a very basic concept on how this is done, but overall I am confused on how I would achieve the boolean part. I do not care too much about performance, I just want the system to work flawlessly, so I can constantly remove and add accounts to the .txt. I am open to other basic files such as .xml ( guest51 , etc), I just am very confused on how to actually "design" the system to do it.

EDIT: I want to make it so I change the boolean value within the .txt of whichever account I'm using. Worst comes to the worst, I'll have to do it in Java (Which I still don't know how to do)

EDIT Again: Or is it possible I could even do it in a folder kind of method, like this: ./user/accounts/test51/

Within that directory, I would create details.txt, where it contained the username, pass, and boolean. The system I want to make will be for like 20 accounts, nothing big

This reads in your example data from a text file, puts the username and password into two strings, and the boolean into a boolean . It doesn't do anything except print this information to screen (there's no writing out to the file at all--this is a read-only example), but it should hopefully get you on your way.

   import  java.io.File;
   import  java.io.IOException;
   import  org.apache.commons.io.FileUtils;
   import  org.apache.commons.io.LineIterator;
   import  org.apache.commons.lang.StringUtils;
/**
   <P>{@code java ReadInActiveAccountsFromFile C:\java_code\username_password_active.txt}</P>
 **/
public class ReadInActiveAccountsFromFile  {
   public static final void main(String[] rqdInputPathInStrArray)  {
      //Read command-line
         String sSrc = null;
         try  {
            sSrc = rqdInputPathInStrArray[0];
         }  catch(IndexOutOfBoundsException ibx)  {
            System.out.println("Missing one-and-only required parameter: The full path to Java source-code file.");
            return;
         }

      //Open input file
         File inputFile = new File(sSrc);
         LineIterator lineItr = null;
         try  {
            lineItr = FileUtils.lineIterator(inputFile);
         }  catch(IOException iox)  {
            System.out.println("Cannot open \"" + sSrc + "\". " + iox);
            return;
         }

      while(lineItr.hasNext())  {
         String line = lineItr.next();
         String[] userPassIsActive = line.split(" ");
         String username = userPassIsActive[0];
         String password = userPassIsActive[1];
         boolean isActive = Boolean.parseBoolean(userPassIsActive[2]);

         System.out.println("username=" + username + ", password=" + password + ", isActive=" + isActive + "");
      }
   }
}

Output:

[C:\java_code\]java ReadInActiveAccountsFromFile C:\java_code\username_password_active.txt
username=guest51, password=password, isActive=true
username=guest52, password=bus, isActive=true
username=guest53, password=password123, isActive=true
username=guest54, password=123password, isActive=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM