First, read the content in the txt text, output it in the console, and directly upload the code:
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));//Construct a BufferedReader class to read the file String s = null; while((s = br.readLine())!=null){//Use the readLine method to read one line at a time 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");//My txt text is stored in the directory, and you can modify it according to your own path System.out.println(txt2String(file)); }}In this way, we read out the data in the txt text, as shown in the following screenshot
Next, how do we take the value line by line and apply it to reality? First upload the code:
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("//input[@id='register-phone']")).sendKeys(tel);//Enter the correct mobile phone number driver.findElement(By.xpath("//input[@id='register-imgcode']")).sendKeys("1234");//Enter the image verification code driver.findElement(By.xpath("//input[@id='register-msgcode']")).sendKeys("123456");//Enter SMS verification code driver.findElement(By.xpath("//input[@id='register-msgcode']")).sendKeys("123456");//Enter SMS verification code driver.findElement(By.xpath("//input[@id='register-password']")).sendKeys("Abc123");//Enter the correct password driver.findElement(By.xpath("//input[@id='register-confirmpassword']")).sendKeys("Abc123");//Enter confirmation password again driver.findElement(By.xpath("//input[@id='agree']")).click();//Check the agree agreement button} }catch(FileNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); }To explain, the tel in the code is the value in the txt text. For example, I want many users to implement registration operations, so I need a new user every time. Here I can use try...catch to achieve it. Because each line of my text content is comma-separated, so split is separated by comma first, and then take one line in the form of an array, until the last line in the txt text ends. Of course, we can apply it to many scenarios that require repeated operations. Here I have automatically implemented several user registration operations, which are very practical and simple, and share them with friends in need!
The above java implementation of reading txt text data and taking values in array form is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.