[英]How to pick set of 5 random values from property file using Java?
我在C:\\Test\\
Directory中有一个名为test.properties
的属性文件。
在Property文件中,我有属性ID和Property值,如下所示:
TestID=Test1,Test2,Test3,Tes4 upto 10
通过使用Java代码,我如何从属性文件中选择任意5个随机值,然后需要验证FE中可用的那些值。
我是Java的初学者,请帮我一些示例代码
我试过的代码如下:
@Test()
public void test() throws Exception{
Properties APPTEXT = new Properties();
Log.info("Getting Input from the Property file");
FileInputStream fs = new FileInputStream("C:\\FilterSection\\dropdown.properties");
APPTEXT.load(fs);
System.out.println ("" +APPTEXT);
Log.info("1. Verifying the Test drop down values are matching in both property file and UI");
String[] expectedDropDownItemsInArray = APPTEXT.getProperty("TestId").trim().split(",");
Random r = new Random();
//System.out.println(expectedDropDownItemsInArray.length);
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
for(int i=0; i<expectedDropDownItemsInArray.length; i++)
expectedDropDownItems.add(expectedDropDownItemsInArray[i]);
System.out.println(expectedDropDownItems+"" );
Thread.sleep(6000);
您读取值并将它们存储在List<String>
。 然后你使用随机播放列表
Collections.shuffle(list);
然后从列表中取出5个第一个元素。
使用随机函数随机rand = new Random();
int idx = rand.nextInt(Array.length); String random =(Array [idx]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.