我正在使用CSV和Java。我知道如何读取文件并从文件中打印值,但不知道如何查找特定值。我试图将值放入arraylist然后使用方法indexof()但是没有工作。有没有办法将价值拉出并打印出来?
答案 0 :(得分:0)
假设您有➜ target git:(master) ✗ java -jar a.jar -b tcp://localhost:61613 -U admin -P password --get --selector "(SYMBOL LIKE 'USDGBP17MAYFUT\!:\!:CDE' ESCAPE '\!')" TEST.QUEUE
-----------------
Message Properties
SYMBOL: USDGBP17MAYFUT::CDE
Payload:
msg
Operation completed in 23ms (excluding connect)
内容:
country.csv
读取此文件并同时过滤所需字段的方法之一。例如,它显示了我们如何按国家/地区代码过滤不同的国家/地区:
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"
<强>输出:强>
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CSVReader {
static Map<String, String> map = new HashMap<String, String>();
public static void main(String[] args) {
String csvFile = "/home/../country.csv";
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
for (int i = 0; i < country.length; i++) {
map.put(country[4], country[5]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
for (Map.Entry<String, String> entry : map.entrySet()) {
String code = entry.getKey();
String name = entry.getValue();
System.out.println("Country [code= " + code + " , name=" + name + "]");
}
}
}