How to access files in a Cuba application directory structure?

How to access files in a Cuba application directory structure ? Could you show me an example ?

Thanks

Hello!

To get access to application files you should use the Resources bean: documentation.

For example, if you want to get a file from the web module you should invoke this:

Resources resources = AppBeans.get(Resources.class);

String path = "classpath:com/haulmont/cuba/web/ExtApp";
InputStream stream = resources.getResourceAsStream(path);

Best regards,
Daniil.

Hi Daniil,

Thank for you reply. Sorry by the late response.

I have a folder called elements inside the core folder of my project, like this:

myProject/modules/core/elements

Inside this folder I have two files: text1.txt and text2.txt

I would like to get the contents of these files. So, to achieve this I have created a method in a Service that tries to get it from one of them:

@Override
public String getFileContents() {
    String folder = "classpath:com/company/myProject/core/elements/text1.txt";

    InputStream inputStream = resources.getResourceAsStream(folder); // this returns null

    try {
        return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    } catch (IOException e) {
        return "Problem accessing file contents";
    }
}

This line is returning null
InputStream inputStream = resources.getResourceAsStream(folder);

I believe the problem is here:
String folder = "classpath:com/company/myProject/core/elements/text1.txt";

Can you help me again on this?

Thanks in advance

James

It seems that everything is OK with your code.

Could you share small project where this problem is reproduced for you?

Regards,
Daniil.