mobileDeveloper
mobileDeveloper

Reputation: 894

read file from res folder blackberry

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?

Upvotes: 0

Views: 941

Answers (2)

Nate
Nate

Reputation: 31045

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)

Upvotes: 0

user784540
user784540

Reputation:

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.

Upvotes: 1

Related Questions