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

首先观察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和对应的label
	static 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和label
					String 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值和对应的label
			System.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">62
0 "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 3
9 5
9 6
10 0
10 2
13 5
13 6
13 9
14 0
14 3
15 0
16 14
17 1
17 6
17 9
17 13
18 15
19 1
19 7
20 8
20 16
20 18
21 18
22 17
24 14
24 15
24 18
25 17
26 1
26 25
27 1
27 7
27 17
27 25
27 26
28 1
28 8
28 20
29 10
29 18
29 21
29 24
30 7
30 19
30 28
31 17
32 9
32 13
33 12
33 14
33 16
33 21
34 14
34 33
35 29
36 1
36 20
36 23
37 8
37 14
37 16
37 21
37 33
37 34
37 36
38 14
38 16
38 20
38 33
39 36
40 0
40 7
40 14
40 15
40 33
40 36
40 37
41 1
41 9
41 13
42 0
42 2
42 10
42 30
43 14
43 29
43 33
43 37
43 38
44 2
44 20
44 34
44 38
45 8
45 15
45 18
45 21
45 23
45 24
45 29
45 37
46 43
47 0
47 10
47 20
47 28
47 30
47 42
49 34
49 46
50 14
50 16
50 20
50 33
50 42
50 45
51 4
51 11
51 18
51 21
51 23
51 24
51 29
51 45
51 50
52 14
52 29
52 38
52 40
53 43
54 1
54 6
54 7
54 13
54 19
54 41
55 15
55 51
56 5
56 6
57 5
57 6
57 9
57 13
57 17
57 39
57 41
57 48
57 54
58 38
59 3
59 8
59 15
59 36
59 45
60 32
61 2
61 37
61 53

转载自:https://blog.csdn.net/firenet1/article/details/51457742

You may also like...