【发布时间】:2015-03-15 14:32:02
【问题描述】:
我有一个 txt 文件,格式如下:
Key:value
Key:value
Key:value
...
我想将所有键及其值放入我创建的 hashMap 中。如何获得 FileReader(file) 或 Scanner(file) 以知道何时在冒号 (:) 处拆分键和值? :-)
我试过了:
Scanner scanner = new scanner(file).useDelimiter(":");
HashMap<String, String> map = new Hashmap<>();
while(scanner.hasNext()){
map.put(scanner.next(), scanner.next());
}
【问题讨论】:
标签: java file text java.util.scanner