package graph;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.FileReader;
import  data.*;import java.util.*;import org.jgrapht.graph.DefaultDirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
public class GraphUtil { DefaultDirectedWeightedGraph<Vertex, DefaultWeightedEdge> g; // graph Map keywords; // keyword_id -> "keyword"
Map keywordIDs; // "keyword" -> keyword_id public void readGraph( String filename ) {
try{
BufferedReader reader = new BufferedReader( new FileReader(filename)); String line = reader.readLine();
int nsize = Integer.parseInt( line );
int count = 0;
Vertex[] nodes = new Vertex[nsize];
Map<Integer, Integer> id2count = new HashMap<Integer, Integer>();

// o.println( "starting nodes"); // process vertices
while( count < nsize ) { // read a new line
line = reader.readLine(); // create a node
Vertex v = new Vertex();
String id = line.substring( 0, line.indexOf(" ") );
v.id = Integer.parseInt( id ); g.addVertex( v );
nodes[ count ] = v;
id2count.put( v.id , count ); //
line = line.substring( line.indexOf(" ") + 1);
String weight = line.substring( 0, line.indexOf(" ") );
v.weight = Float.parseFloat( weight ); line = line.substring( line.indexOf(" ") + 1 );
while( line.indexOf(" ") != -1 ) {
String key = line.substring( 0, line.indexOf( " " ) );
v.addKey( Integer.parseInt( key ) );
line = line.substring( line.indexOf(" ") + 1 );
} count++;

// if( count % 10000 == 0 ) {
// o.println( "reading nodes " + count );
// }
}

o.println( "starting edges");
count = 0;
// process edges
while( (line = reader.readLine()) != null ) {
String srcid = line.substring(0, line.indexOf(" "));
line = line.substring( line.indexOf(" ") + 1);
String tgtid = line.substring( 0, line.indexOf(" ") );
line = line.substring( line.indexOf(" ") + 1);
Vertex src = nodes[ id2count.get( Integer.parseInt( srcid ) ) ];
Vertex tgt = nodes[ id2count.get( Integer.parseInt( tgtid ) ) ]; DefaultWeightedEdge e = g.addEdge( src , tgt );
g.setEdgeWeight( e , Float.parseFloat( line ) );

count++;
if( count % 50000 == 0 ) {
o.println( "reading edge " + count);
}
} }
catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
GraphUtil gu = new GraphUtil(); gu.g = new DefaultDirectedWeightedGraph<Vertex, DefaultWeightedEdge>( DefaultWeightedEdge.class );
try{
gu.readGraph( "dblp.graph");
    gu.saveGraph( "mydblp" );
    }
catch(Exception e){
e.printStackTrace();
}
}
}
为了不至于代码繁琐,我只是将readGraph方法写出来了。
我用的是Myeclipse8.0,当前目录下也有一个文件“dblp.gragh”。但是总是会报一个:
java.io.FileNotFoundException: dblp.graph (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
总是说找不到指定的文件。不知道是为什么?请各位大虾指教啊