How do I load a large custom dictionary into my node at runtime?
I am doing some natural language processing and I have a large Chinese dictionary that I want to load into my custom node. Here's my code:
URL urlChineseDictionary = this.getClass().getResource("../data/ChineseDictionary.txt"); try { URI uriChineseDictionary = urlChineseDictionary.toURI(); File fileChineseDictionary = new File( uriChineseDictionary ); BufferedReader fileBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(fileChineseDictionary), "UTF8") ); while( ( readLine = fileBufferedReader.readLine() ) != null ){ //read the file line by line and load it into an internal data structure
While this seems to be the standard way Java reads in files, it doesn't seem to work with KNIME when the node is a plugin. The exception I get is:
"URI scheme is not file"
In fact, the URI says that it is a bundleresource (bundleresource://629.fwk551897230/com/...)
I found another note that says: "This is not possible using a file path, as all resources in a plugin are resolved using a bundle relative URL (and the bundle itself might be deployed as jar file, so there even isn't a normal file)".
I also found this other link (http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/) that seems useful. It says:
Frequenty you want to store static files in your bundle and load them from your bundle. For this you can use the following code:
URL url; try { url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt"); InputStream inputStream = url.openConnection().getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch (IOException e) { e.printStackTrace(); }
So the trick seems to be the URL right. But what is a typical URL for KNIME nodes? How do I figure out the URL that will point to my text file from within the node plugin?