亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值

 更新時(shí)間:2018年07月13日 09:16:37   作者:征途無境  
今天小編就為大家分享一篇java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

首先來讀取txt文本中的內(nèi)容,輸出在控制臺(tái),直接上代碼:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
 
public class ReadFiledata {
 public static String txt2String(File file){
  StringBuilder result = new StringBuilder();
  try{
   BufferedReader br = new BufferedReader(new FileReader(file));//構(gòu)造一個(gè)BufferedReader類來讀取文件
   String s = null;
   while((s = br.readLine())!=null){//使用readLine方法,一次讀一行
    result.append(System.lineSeparator()+s);
   }
   br.close(); 
  }catch(Exception e){
   e.printStackTrace();
  }
  return result.toString();
 }
 
 public static void main(String[] args){
  File file = new File("F:/card.txt");//我的txt文本存放目錄,根據(jù)自己的路徑修改即可
  System.out.println(txt2String(file));
 }
}

這樣我們就把txt文本中的數(shù)據(jù)讀出來了,如下截圖所示

接下來我們?cè)趺粗鹦腥≈蛋阉〕鰜聿?yīng)用到實(shí)際中呢?先上代碼:

try{
 String s = "";
   BufferedReader in =new BufferedReader(new FileReader("F:\\tel.txt"));
   while((s=in.readLine())!=null){
   String[] split = s.split(",");
   String tel = split[0];
   driver.findElement(By.xpath("http://input[@id='register-phone']")).sendKeys(tel);//輸入正確手機(jī)號(hào)
   driver.findElement(By.xpath("http://input[@id='register-imgcode']")).sendKeys("1234");//輸入圖片驗(yàn)證碼
   driver.findElement(By.xpath("http://input[@id='register-msgcode']")).sendKeys("123456");//輸入短信驗(yàn)證碼
   driver.findElement(By.xpath("http://input[@id='register-password']")).sendKeys("Abc123");//輸入正確密碼
   driver.findElement(By.xpath("http://input[@id='register-confirmpassword']")).sendKeys("Abc123");//再次輸入確認(rèn)密碼
   driver.findElement(By.xpath("http://input[@id='agree']")).click();//勾選同意協(xié)議按鈕
 } 
 }catch(FileNotFoundException e){
 e.printStackTrace();
   }
 catch(IOException e){
   e.printStackTrace();
   }

說明一下,代碼中的tel就是txt文本中的值,比如我要很多用戶實(shí)現(xiàn)注冊(cè)操作,那么我每次都需要新的用戶,這里用try...catch可以實(shí)現(xiàn),因?yàn)槲业奈谋緝?nèi)容每一行是有逗號(hào)分隔的,所以先split以逗號(hào)分隔一下,然后再以數(shù)組形式,每次取一行,直到取完txt文本中最后一行結(jié)束。當(dāng)然我們可以應(yīng)用到很多需要重復(fù)操作的場(chǎng)景中,這里我自動(dòng)化實(shí)現(xiàn)了若干用戶注冊(cè)的操作,很實(shí)用很簡(jiǎn)單,分享給有需要幫助的朋友!

以上這篇java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論