One of your services needs a content of a file located at your web application's context folder. More specifically, the file we want to read is in the config folder under the context directory.
AppModule.java
1 public static TownNames buildTownNames(Context context, final Logger log) {
2 return new TownNamesImpl(context, log);
3 }
TownNames.java is a very simple interface:
1 public interface TownNames {
2 List<String> getAll();
3 }
And the implementation (TownNamesImpl.java) which demonstrates reading a file named towns.txt from the config folder under the web app context folder:
1 public class TownNamesImpl implements TownNames {
2
3 private List<String> townNames = new ArrayList<String>();
4
5 public TownNamesImpl(Context context, Logger log) {
6 String town = null;
7 Resource configFile = new ContextResource(context, "config/towns.txt");
8 try {
9 InputStream is = configFile.openStream();
10 BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));
11
12 while ((town = br.readLine()) != null) {
13 townNames.add(town);
14 }
15
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19
20 }
21
22 public List<String> getAll() {
23 Collections.sort(townNames);
24 return Collections.unmodifiableList(townNames);
25 }
26
27 }