How to read a JSON file that is inside a package?

2

I need to give a file in the *.JSON case. I understand that there is a library for this in case I use json-simple-1.1.1 . As it is necessary to read the file first and then work with it.

My question is as follows:

I keep the file.json file inside a com.app.json package and need to access that file.

What would be the most efficient method to access this file within the package?

Note:

This should apply to files .json , .txt , .xml , and others.

    
asked by anonymous 17.02.2014 / 18:35

1 answer

2

It seems to me that the package where the files will be will always be the same. So, just use the code below:

Reader in = new InputStreamReader(getClass().getResourceAsStream("/com/app/json/file.json") );
Object o = JSONValue.parse(in);

Otherwise, you can write a function that receives the package name and transforms it into a valid path within the classpath.

Hugs

    
17.02.2014 / 20:22