如何使用属性进行while循环我在Iterator上玩了2天,toString解析和其他很多形式就是我要显示的格式。仅适用于一组属性,因此只显示查询中的最后一项。
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while((line=in.readLine())!=null){
System.out.println(line);
}
以错误的格式显示2套
现在这是完美的格式,但只打印最后一项:
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
Properties prop= new Properties();
while((line=in.readLine())!=null){
prop.load(in);
String amount = prop.getProperty("ssl_amount");
String card = prop.getProperty("ssl_card_number");
String approval = prop.getProperty("ssl_approval_code");
String results = prop.getProperty("ssl_result_message");
String time = prop.getProperty("ssl_txn_time");
System.out.println(amount+" "+card+" "+approval+" "+results+" "+time+" ");
}
如何制作循环以正确的格式打印所有条目? 这是原始输入流:
ssl_txn_count=2
ssl_txn_id=071214A15-B805A410-E9F4-4D6D-AA87-0E9923FBC7FA
ssl_user_id=webpage
ssl_trans_status=STL
ssl_card_type=CREDITCARD
ssl_transaction_type=SALE
ssl_txn_time=12/07/2014 11:28:00 PM
ssl_first_name=
ssl_last_name=
ssl_card_number=41**********1111
ssl_exp_date=1115
ssl_entry_mode=K
ssl_avs_response=
ssl_cvv2_response=P
ssl_amount=10.00
ssl_invoice_number=
ssl_result_message=APPROVAL
ssl_approval_code=CVI064
ssl_txn_id=061214A15-8921B6B0-FF9E-4DA5-97D1-288C28272B10
ssl_user_id=webpage
ssl_trans_status=STL
ssl_card_type=CREDITCARD
ssl_transaction_type=SALE
ssl_txn_time=12/06/2014 01:25:18 AM
ssl_first_name=
ssl_last_name=
ssl_card_number=41**********1111
ssl_exp_date=1215
ssl_entry_mode=K
ssl_avs_response=
ssl_cvv2_response=P
ssl_amount=12.00
ssl_invoice_number=
ssl_result_message=APPROVAL
ssl_approval_code=CVI806
这是我想要的好格式:(就像你只能看到报告1而不是2)
run:
12.00 41**********1111 CVI806 APPROVAL 12/06/2014 01:25:18 AM
BUILD SUCCESSFUL (total time: 1 second)
答案 0 :(得分:1)
加载Reader
一次(在循环外),然后您可以迭代keySet
。接下来,您可以显示键和值。使用可能类似的Format String Synatx,
try (InputStreamReader is = new InputStreamReader(
conn.getInputStream(), "UTF-8")) {
Properties prop = new Properties();
prop.load(is);
for (Object key : prop.keySet()) {
System.out.printf("%s=%s%n", key, prop.get(key));
}
}
对于硬编码密钥(您的特定情况以及如果您想忽略其他属性),您不需要循环
try (InputStreamReader is = new InputStreamReader(
conn.getInputStream(), "UTF-8")) {
Properties prop = new Properties();
prop.load(is);
String amount = prop.getProperty("ssl_amount");
String card = prop.getProperty("ssl_card_number");
String approval = prop.getProperty("ssl_approval_code");
String results = prop.getProperty("ssl_result_message");
String time = prop.getProperty("ssl_txn_time");
System.out.println(amount + " " + card + " " + approval + " "
+ results + " " + time + " ");
}
以上是使用try-with-resources
完成后关闭流。
答案 1 :(得分:0)
使用Enumuration迭代属性文件,这是一个示例
Properties prop = new Properties();
InputStream input = nnew FileInputStream("<file-location>.properties");
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}