如何使用 Java 从属性文件中选择一组 5 个随机值? [英] How to pick set of 5 random values from property file using Java?

查看:33
本文介绍了如何使用 Java 从属性文件中选择一组 5 个随机值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C:\Test\ 目录中有一个名为 test.properties 的属性文件.

I have a Property file called test.properties in C:\Test\ Directory.

在属性文件中,我有 property IDProperty 值如下:

In the Property file i have property ID and Property values as follows:

TestID=Test1,Test2,Test3,Tes4 upto 10

通过使用 Java 代码,我如何从属性文件中选择任意 5 个随机值,然后需要验证这些值在 FE 中是否可用.

By using Java code how can i pick any 5 random values from the property file and then need to verify the those values are available in the FE.

我是 Java 的初学者,请帮助我提供一些示例代码

I'm beginner of Java please help me with some sample code

我试过的代码如下:

@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 中.然后使用

You read the values and store them in a List<String>. Then you shuffle the list using

Collections.shuffle(list);

然后从列表中取出前 5 个元素.

Then you take the 5 first elements from the list.

这篇关于如何使用 Java 从属性文件中选择一组 5 个随机值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆