gml文件读写 hashmap用法与遍历 以及 文本文件解析方法

来源:互联网 发布:淘宝客服售后要点 编辑:程序博客网 时间:2024/06/04 12:34

首先观察gml文件的存储格式

Creator "Mark Newman on Wed Jul 26 15:04:20 2006"graph[  directed 0  node  [    id 0    label "Beak"  ]  node  [    id 1    label "Beescratch"  ]  edge  [    source 61    target 37  ]  edge  [    source 61    target 53  ]]
可以看出存储格式graph表示一个图,然后 [  ] 用来 指定图中的元素

node指定一个节点,并且[]中说明该节点的id属性和label属性

edge一样,有一个起点和终点的id。

于是用java按行读取,删除开头和末尾的空格,再用split函数按空格分成字符串

如果第一个字符串是id,那么读取下一行,这样就有了id值和label值,此时便获取了节点信息了。

当然没有删除 “” 符号,这个可以进一步处理

边也是这样处理的,对于字符串转整数,直接调用函数即可。


package biger;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class gml {static int[][]edge = new int[2000][2]; //存储边//存储id和对应的labelstatic Map<Integer,String> idToString = new HashMap<Integer,String>();public static void main(String[] args) throws IOException      {  String filePath = "C:\\dolphins.gml";File file = new File(filePath);int edgenum = 0; //记录边数if(file.isFile() && file.exists()){InputStreamReader read = new InputStreamReader(new FileInputStream(file));BufferedReader buffer = new BufferedReader(read);String type;int value;while((type = buffer.readLine()) != null){type = type.trim();String x[] = type.split(" ");if(x[0].equals("id")){ //读取id和labelString second = buffer.readLine();second = second.trim();String x2[] = second.split(" ");idToString.put(Integer.parseInt(x[1]),x2[1]);}else if(x[0].equals("source")){//读取边String second = buffer.readLine();second = second.trim();String x2[] = second.split(" ");edge[edgenum][0] = Integer.parseInt(x[1]);edge[edgenum][1] = Integer.parseInt(x2[1]);edgenum++;}}//输出所有的id值和对应的labelSystem.out.println(idToString.size());Iterator<Integer> iter = idToString.keySet().iterator();while (iter.hasNext()) {Integer key = (Integer) iter.next();String val = (String) idToString.get(key);System.out.println(key+" "+val);}//输出所有边for(int i = 0;i < edgenum; i++){System.out.println(edge[i][0]+" "+edge[i][1]);}}else {System.out.println("找不到指定文件");}    }}



运行结果为:


</pre><pre name="code" class="java">620 "Beak"1 "Beescratch"2 "Bumper"3 "CCL"4 "Cross"5 "DN16"6 "DN21"7 "DN63"8 "Double"9 "Feather"10 "Fish"11 "Five"12 "Fork"13 "Gallatin"14 "Grin"15 "Haecksel"16 "Hook"17 "Jet"18 "Jonah"19 "Knit"20 "Kringel"21 "MN105"22 "MN23"23 "MN60"24 "MN83"25 "Mus"26 "Notch"27 "Number1"28 "Oscar"29 "Patchback"30 "PL"31 "Quasi"32 "Ripplefluke"33 "Scabs"34 "Shmuddel"35 "SMN5"36 "SN100"37 "SN4"38 "SN63"39 "SN89"40 "SN9"41 "SN90"42 "SN96"43 "Stripes"44 "Thumper"45 "Topless"46 "TR120"47 "TR77"48 "TR82"49 "TR88"50 "TR99"51 "Trigger"52 "TSN103"53 "TSN83"54 "Upbang"55 "Vau"56 "Wave"57 "Web"58 "Whitetip"59 "Zap"60 "Zig"61 "Zipfel"8 39 59 610 010 213 513 613 914 014 315 016 1417 117 617 917 1318 1519 119 720 820 1620 1821 1822 1724 1424 1524 1825 1726 126 2527 127 727 1727 2527 2628 128 828 2029 1029 1829 2129 2430 730 1930 2831 1732 932 1333 1233 1433 1633 2134 1434 3335 2936 136 2036 2337 837 1437 1637 2137 3337 3437 3638 1438 1638 2038 3339 3640 040 740 1440 1540 3340 3640 3741 141 941 1342 042 242 1042 3043 1443 2943 3343 3743 3844 244 2044 3444 3845 845 1545 1845 2145 2345 2445 2945 3746 4347 047 1047 2047 2847 3047 4249 3449 4650 1450 1650 2050 3350 4250 4551 451 1151 1851 2151 2351 2451 2951 4551 5052 1452 2952 3852 4053 4354 154 654 754 1354 1954 4155 1555 5156 556 657 557 657 957 1357 1757 3957 4157 4857 5458 3859 359 859 1559 3659 4560 3261 261 3761 53






0 0
原创粉丝点击