解析和读取文本文件中的数据 [英] Parse and read data from a text file
本文介绍了解析和读取文本文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的文本文件中的数据具有以下格式
I have data in my text file in the following format
apple fruit
carrot vegetable
potato vegetable
我想逐行阅读并在第一个空格处分割并将其存储在set或map或java的任何类似集合中. (键和值对)
I want to read this line by line and split at the first space and store it in a set or map or any similar collections of java. (key and value pairs)
示例:-
"apple fruit"
应该存储在地图中
key = apple
和
value = fruit
.
example :-
"apple fruit"
should be stored in a map where the
key = apple
and
value = fruit
.
推荐答案
扫描仪类可能就是您想要的.
The Scanner class is probably what you're after.
例如:
Scanner sc = new Scanner(new File("your_input.txt"));
while (sc.hasNextLine()) {
String line = sc.nextLine();
// do whatever you need with current line
}
sc.close();
这篇关于解析和读取文本文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文