0

I want to read file from "res" folder on blackberry. The file that i used is a file javascript. I used this code InputStream in = classs.getResourceAsStream("file.js");. But i get "could not find this path" and I use also

String srcFile = "/res/ressourcesWeb/file.js";
FileConnection srcConn = (FileConnection) Connector.open(srcFile, Connector.READ);
InputStream in = srcConn.openInputStream();

but i got an exception. Can any one help me to read the file and give me the right path that should I use?

Nate
  • 31,017
  • 13
  • 83
  • 207
mobileDeveloper
  • 894
  • 2
  • 14
  • 35

2 Answers2

1

Your res folder has to be inside src folder to be accessed from your code.

src folder is the root folder of your project package. And all folders outside of src folder are invisible for the code at runtime.

Check this post for more details: Blackberry runtime error: FRIDG: could not find img/logo.png

There's file location principle described.

Community
  • 1
  • 1
  • Actually, you **don't** need to put your `res` folder under `src`. That's one way to solve the problem, but I would not recommend that, for organization purposes. – Nate Nov 23 '12 at 21:30
  • A developer can have res folder outside, but copy it's contents to src folder on preprocessing stage. –  Nov 24 '12 at 10:43
  • There is no need to copy anything. I've done this both with the old JDE, and the Eclipse plug-in. Saying that the `res` folder has to be inside the `src` folder is incorrect. The poster simply wasn't using the correct code to access his JavaScript resource. See [here for another example of doing this](http://stackoverflow.com/a/11551148/119114). – Nate Nov 29 '12 at 12:06
0

You actually do not need to put your resources under the src folder for them to be accessible from your code.

That is one way to do it, but I don't think it's the best way. Files under the src folder should really be source code, not images, or other resources. For JavaScript resources, it's debatable whether those should be under src or not. Most projects I've seen have used the src folder for only Java source code.

In any case, if you would like to keep your file (or other resources, like images) outside the src folder, you can do so. The BlackBerry plugin for Eclipse actually sets it up like this by default, when you create a new project. There is a res folder at the top level, next to (not under) src.

If you have

src\
src\com\mycompany\myapp\
res\
res\resourcesWeb\
res\resourcesWeb\file.js

Then, you can open the file like this:

    String jsPath = "/resourcesWeb/file.js";
    InputStream input = getClass().getResourceAsStream(jsPath);
    byte [] content = IOUtilities.streamToBytes(input);
    String contentAsString = new String(content);

P.S. You also can probably do this:

    String jsPath = "/file.js";
    InputStream input = getClass().getResourceAsStream(jsPath);

and not specify the path to the resource. Obviously, this will only work if there are no naming conflicts in your resource folders (e.g. you don't have /res/resourcesWeb/file.js and also /res/otherPath/file.js)

Nate
  • 31,017
  • 13
  • 83
  • 207