传递2个字符串中的值并将其从.txt文件中删除

时间:2018-03-27 09:09:22

标签: java string

我有一个问题,我在2个字符串中传递2个值(Facebook用户名和Facebook密码),然后从.txt文件中删除它们。

这是我的代码:

private static String[] fbuser;
private static String[] fbpass;
FileReader fr;
BufferedReader br;
PrintWriter pw;     
String tmp;
int FileContentCount;
int noOfUsers = 3;

public void insertFacebookDetails() throws InterruptedException {

    fbuser = new String[noOfUsers];
    fbpass = new String[noOfUsers];

    String UserFileName = "fbusers.txt";

    try {           
        fr = new FileReader(UserFileName);
        br = new BufferedReader(fr);
        FileContentCount = 0;

        while ((tmp = br.readLine()) != null) {             
            fbuser[FileContentCount] = tmp;
            tmp = br.readLine();
            fbpass[FileContentCount] = tmp;
            FileContentCount++;             
        }
        br.close();
        fr.close();
        System.out.println(fbuser[0] + ": " + fbpass[0]);

        pw = new PrintWriter(UserFileName);
        int count = 1;
        while (count < FileContentCount) {
            pw.write(fbuser[count] + "\n");
            pw.write(fbpass[count] + "\n");
            count++;
        }
        pw.flush();
        pw.close();

    }
    catch (Exception e) {
        System.out.println(e);
    }       
    for (String fbLogin : driver.getWindowHandles()) {
        driver.switchTo().window(fbLogin);  
    }
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(EMAIL)));           
    driver.findElement(By.id(EMAIL)).sendKeys(fbuser);
    driver.findElement(By.id(PASSWORD)).sendKeys(fbpass);
    Thread.sleep(2000);
    driver.findElement(By.name(LOGIN)).click(); 
}

我按顺序在.txt文件中有用户:

  • fb_user1
  • fb_pass1
  • fb_user2
  • fb_pass2
  • fb_user3
  • fb_pass3
  • fb_user4等等

当它找到电子邮件ID时,它实际上发送了3个用户(因为noOfUsers值为3),但是如果我把一个低于该值的数字,它将无法工作。 (得到arrayoutofbounds错误)

第二个问题是它不会从.txt文件中删除用户。

我也不知道在哪里放下以下代码:

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(EMAIL)));           
driver.findElement(By.id(EMAIL)).sendKeys(fbuser);
driver.findElement(By.id(PASSWORD)).sendKeys(fbpass);
Thread.sleep(2000);
driver.findElement(By.name(LOGIN)).click(); 

我知道在读取它之前和删除之前它需要在某个地方。我尝试了所有可能的方法,但我很困惑。

有人可以帮我吗?

编辑: 这是代码在抽象类中的外观

public static void readFile() {
     try(BufferedReader reader = new BufferedReader(new FileReader(fileName))){
          String tmp;
          while((tmp = reader.readLine())!= null){
            fbUsers.add(tmp);
            System.out.println(tmp);
          }
          System.out.println(fbUsers.size());
        } catch (IOException e) {
            System.out.println(e);
        }
}

public static void writeFile() {
    try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
          for(String fbUser : fbUsers){
            writer.write(fbUser);
            writer.newLine();
          }
        } catch (IOException e) {
            System.out.println(e);
        }
}

@SuppressWarnings("unused")
public void insertFacebookDetails(String fbUser) throws InterruptedException {  
    readFile();
    for (String fbLogin : driver.getWindowHandles()) {
        driver.switchTo().window(fbLogin);  
    }
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(EMAIL)));   
    String username = fbUser.split("/")[0];
    String password = fbUser.split("/")[1];
    fbUsers.remove(fbUser);
    driver.findElement(By.id(EMAIL)).sendKeys(username);
    driver.findElement(By.id(PASSWORD)).sendKeys(password);
    Thread.sleep(2000);
    driver.findElement(By.name(LOGIN)).click(); 
    writeFile();
}

这是我在另一个类中调用该方法的地方:

public void step1() {
   insertFacebookDetails(fbUsers.get(0));
}

1 个答案:

答案 0 :(得分:0)

以下是如何从文件中读取用户并从您登录的文件中删除用户的示例:
首先,用户将这样存储在fbusers.txt文件中:

  

fb_user1 / fb_pass1
fb_user2 / fb_pass2
fb_user3 / fb_pass3
  等...

然后我们使用String[]代替List<String>,因为它会自动展开。因此,我们不会通过向其添加fbUsers来获得arrayOutOfBounds异常。

首先,我们从文件中读取所有fbUsers:

public static void readFile(){
    try(BufferedReader reader = new BufferedReader(new FileReader(fileName))){
      String tmp;
      while((tmp = reader.readLine())!= null){
        fbUsers.add(tmp);
        System.out.println(tmp);
      }
    } catch (IOException e){
      e.printStackTrace();
    }
  }

所以现在所有的fbUsers都以格式存储在我们的列表中:fb_user1/fb_pass1

现在我们将使用我们的一个fb_users登录:

public static void login(String fbUser){
    String userName = fbUser.split("/")[0]; //get UserName from fbUser
    String password = fbUser.split("/")[1]; //get Password from fbUser
    fbUsers.remove(fbUser); //remove the fbUser with which we have logged in

    //code to login
  }

最后,我们编写的fbUsers没有我们在文件中记录的那个:

public static void writeFile(){
    try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
      for(String fbUser : fbUsers){
        writer.write(fbUser);
        writer.newLine();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

这是主要方法:

private static final String fileName = "fbusers.txt";
private static final List<String> fbUsers = new ArrayList<>();

public static void main(String[] args){
    readFile();
    login(fbUsers.get(0));
    writeFile();
  }

希望它可以帮助你,你可以继续这个例子。