36 lines
1.1 KiB
Java
36 lines
1.1 KiB
Java
import org.jgrapht.graph.SimpleWeightedGraph;
|
|
import org.jgrapht.nio.dot.DOTExporter;
|
|
import org.jgrapht.nio.graphml.GraphMLExporter;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
public class GraphMLFileWriter {
|
|
|
|
String filename;
|
|
SimpleWeightedGraph graph;
|
|
|
|
|
|
public GraphMLFileWriter(String filename, SimpleWeightedGraph graph) {
|
|
if(!filename.matches(".*\\.graphml")){
|
|
filename = filename + ".graphml";
|
|
}
|
|
this.filename = filename;
|
|
this.graph = graph;
|
|
}
|
|
|
|
public void writeGraphToFile() {
|
|
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
|
){
|
|
GraphMLExporter<SimpleWeightedGraph, BufferedWriter> exporter = new GraphMLExporter<>();
|
|
exporter.exportGraph(graph, writer);
|
|
} catch(IOException ex){
|
|
System.out.println("Could not make new file named "+filename);
|
|
System.err.println(ex);
|
|
}
|
|
}
|
|
}
|