搜索用java用java
在一個文件中的txt這是我的txt文件搜索的txt文件中的
「hhhead」: 「梅爾文」, 「答辯人」: 「Edlen」
我想尋找hhhead和程序將返回梅爾文
我該怎麼辦呢?
搜索用java用java
在一個文件中的txt這是我的txt文件搜索的txt文件中的
「hhhead」: 「梅爾文」, 「答辯人」: 「Edlen」
我想尋找hhhead和程序將返回梅爾文
我該怎麼辦呢?
我假設您已準備好讀取.txt
文件並寫入字符串的代碼。
在下面的代碼中,str是具有文本文件內容的字符串。
String str = "\"hhhead\":\"Melvin\",\"respondent\":\"Edlen\",\"int_date\":\"2017-06-1\u200C\u200B5\",\"start_time\":\"09:\u200C\u200B46\",\"interviewer\":\"K\u200C\u200Bit\",\"house_type\":\"1\"\u200C\u200B,\"nbr\":2,\"nstorey\":1\u200C\u200B,\"roof\":\"1\",\"wall\":\"\u200C\u200B2\",\"floor\":\"7\",\"nnuc\u200C\u200Bfam\":1,\"phsize\":5,\"h\u200C\u200Bpq_mem\":[{\"memno\":\"1\u200C\u200B\",\"msname\":\"Malabarb\u200C\u200Bas\",\"mfname\":\"Melvin\u200C\u200B\",\"mmname\":\"Pirneto\"\u200C\u200B,\"reln\":\"1\",\"reln_o\"\u200C\u200B:\"\",\"nucfam\":\"1\",\"se\u200C\u200Bx\":\"1\",\"birth_date\":\u200C\u200B\"1969-12-18\"";
Map<String,String> map = new HashMap<String,String>();
String[] split = str.split(",");
for(String s: split){
String[] split2 = s.split(":");
map.put(split2[0], split2[1]);
}
System.out.println(map.get("\"hhhead\""));
我從你的意見中複製的數據,不得不使用反斜槓(「\」)逸出串,使用具有分裂方法「」作爲參數,並存儲在對一個鍵 - 值映射。如果您使用密鑰進行搜索,"\"hhhead\""
您將獲得"Melvin"
。
希望這會有所幫助!
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
String Seach="hhhead";
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.contains(Seach)){
System.out.println("hhhead FOUND!!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
你可以很容易地使用'String.split(「,」)'和'Map'作爲數據。明智的代碼是什麼? – vikingsteve
迄今沒有。我只想在代碼之前思考。 – Shaine
「hhhead」:「Melvin」,「respondent」:「Edlen」,「int_date」:「2017-06-15」,「start_time」:「09:46」,「interviewer」:「Kit」,「house_type」 : 「1」, 「NBR」:2 「nstorey」:1, 「屋頂」: 「1」, 「壁」: 「2」, 「地板」: 「7」, 「nnucfam」:1, 「phsize」 :5, 「hpq_mem」:[{ 「memno」: 「1」, 「msname」: 「Malabarbas」, 「mfname」: 「梅爾文」, 「mmname」: 「Pirneto」, 「RELN」: 「1」,」 reln_o「:」「,」nucfam「:」1「,」sex「:」1「,」birth_date「:」1969-12-18「 – Shaine