Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File reading example doesn't work #366

Open
matwell-dev opened this issue Jan 5, 2022 · 0 comments
Open

File reading example doesn't work #366

matwell-dev opened this issue Jan 5, 2022 · 0 comments

Comments

@matwell-dev
Copy link

The example here: https://graphstream-project.org/doc/Tutorials/Reading-files-using-FileSource/ doesn't work. The construction of the file-source can throws an IOException.

Also, not a bug, but the example might as well display the constructed graph (I've defaulted it to Swing as a lowest-common denominator, as per some of the other examples), so I've added the code for that and tightened up the clean-up code in the case of an exception and the FileSource not being constructed.

At the risk of over-engineering an example, I might also have added only initialising the UI choice if not already defined, and also taking a list of (at least one) file-name parameters and processing them from the command-line, but it's your code! :o)

import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.stream.file.FileSource;
import org.graphstream.stream.file.FileSourceFactory;

import java.io.IOException;

public class TutorialFileSource {

public static void main(String ... args) {
	String filePath = "<<<YOUR-FILENAME-HERE>>>";

	System.setProperty("org.graphstream.ui", "swing");

	Graph g = new DefaultGraph("g");
	FileSource fs = null;

	try {
		fs = FileSourceFactory.sourceFor(filePath);

		fs.addSink(g);

		fs.begin(filePath);

		while (fs.nextEvents()) {
			// Optionally some code here ...
		}

		g.display();

	} catch( IOException e) {
		e.printStackTrace();
	}

	try {
		if (fs != null) fs.end();
	} catch( IOException e) {
		e.printStackTrace();
	} finally {
		if (fs != null) fs.removeSink(g);
	}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant