using knime workspace location in java snippet

Hi,

In my workflows I use a lot of Java Snippets. Inside those snippets I often use FileInputStream to open files residing in my local file system (i run knime on a 64bit windows computer under windows 8.1).

My question: Is it possible to refer to a file location inside a java snippet using a path residing in the knime workspace, i.e. e.g. knime://LOCAL/mypath/myfile.cfg

I tried e.g. new FileInputStream(new File("knime://LOCAL/mypath/myfile.cfg"))

but the call fails. (of course the file does reside in the correct location)

Since referencing a file via an absolute path makes distribution of my workflows much harder, i would appreciate if someone can help.

 

Best regards

Hans-Juergen

 

 

 

Hi Hans-Juergen,

is there a specific reason you propose to use the Java Snippet instead of the native KNIME File Reader nodes?

No this is not supported. The best workaround is to use the native File nodes.

Kind regards, Iris

You can use the flow variable of the workspace to create a local URI for the file. It does not look as nice as the "knime://LOCAL/mypath/myfile.cfg" URI, but with the following snippet (you need to add the imports too):

try {
     URL url = new URL(new File(v_knimeworkspace).toURI().toURL(), "mypath/myfile.cfg");
     //Do something with the url:
     logInfo(url.toString());
} catch (Exception e) {
	logFatal(e.getMessage(), e);
        //Stop on error
	throw new Abort(e);
}

 

Thanks for your hint. Will try it out.

Enjoy your day.

Cheers

Hans-Juergen