首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java bufferedreader读取

Java bufferedreader读取
EN

Stack Overflow用户
提问于 2014-03-02 11:53:06
回答 1查看 519关注 0票数 0

我正在尝试制作一种不同类型的登录系统。我想让它读取一个.txt文件,其中包含如下详细信息(格式为用户名-密码-布尔值):

代码语言:javascript
复制
guest51 password true
guest52 bus true
guest53 password123 true
guest54 123password false

然而,这就是它变得非常困难的地方。然后我想检查布尔值,如果它是"true",跳过帐户,如果不是,使用它。从那里,当它“完成”帐户时,将其设置为"true“。

我希望用法是这样的:

代码语言:javascript
复制
username = nextAvaialableAccount.getUsername();
password = nextAvailableAccount.getPassword();

我对如何做到这一点有一个非常基本的概念,但总的来说,我对如何实现布尔部分感到困惑。我不太关心性能,我只希望系统能够完美地工作,这样我就可以不断地删除和添加帐户到.txt。我对其他基本文件如.xml ( guest51等)持开放态度,我只是对如何“设计”系统才能做到这一点感到非常困惑。

编辑:我想让它这样做,所以我更改了我使用的任何帐户的.txt中的布尔值。在最坏的情况下,我不得不用Java (我仍然不知道该怎么做)

再次编辑:或者我甚至可以在文件夹类型的方法中进行编辑,比如:./user/account/test51/

在该目录中,我将创建details.txt,其中包含用户名、pass和布尔值。我想要做的系统将是大约20个帐户,没有大

EN

回答 1

Stack Overflow用户

发布于 2014-03-02 12:06:13

这将从文本文件中读取示例数据,将用户名和密码放入两个字符串中,并将布尔值放入boolean中。除了将这些信息打印到屏幕上之外,它什么也不做(根本没有写到文件中--这是一个只读示例),但希望它能帮助您上路。

代码语言:javascript
复制
   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 + "");
      }
   }
}

输出:

代码语言:javascript
复制
[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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22123612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档