Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Reading packaged files in webwork action

volalex
Contributor
July 3, 2011

Hi, I have a question about how confluence storing package files.

In example i have a webwork action MyAction and some file named as myfile.txt

package for this objects is

<root>/src/main/java/com/example/actions/MyAction.java

<root>/src/main/resources/text/myfile.txt

How I can refer to myfile.txt in MyAction?

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 4, 2011

Assuming you are writing a Confluence plugin, and the myfile.txt is included in your plugin jar, you can use the MyAction's ClassLoader to get the contents of the text file.

Something like this:

InputStream myFile = this.getClass().getClassLoader().getResourceAsStream("text/myfile.txt");

CharlesA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 4, 2011

This is correct. However I would add that if you are using this method to load a file, most classloaders will cache the contents of the file in memory for the lifetime of the classloader. We've run into this problem a few times in Confluence where we use resource streams to load large data files that we only need during startup.

If you're worried about the object you're loading being cached, you can instead use the getResource() method to return a URL to the file, then open that URL independently.

volalex
Contributor
July 4, 2011

First method will be good. Thank You!

TAGS
AUG Leaders

Atlassian Community Events