Context:
I have a webapp where I want to provide the feature of auto complete in a text box. I have kept a "dictionary.txt" file in the resources folder of my web app project (the proj structure mentioned below).
Web app structure
mywebapp
|----src
|----main
|----java
|----resources
|----dictionary.txt
Problem: I am trying to load the "dictionary.txt" file in the constructor of one of my controllers when the application gets deployed. However I am am getting a FileNotFoundException despite the file being placed there. Please refer the sourcecode below:
@Controller
MyCtrl {
public String dictionaryFile="dictionary.txt";
@Autowired
private MyAutocompleteDictTrie dictTrie;
public MyCtrl(){
DictionaryLoader.loadDictionary(dictTrie,dictionaryFile);
}
// remaining controller business code here
}
public class DictionaryLoader{
public static void loadDictionary(MyAutocompleteDictTrie trie,fileName){
BufferedReader br=null;
try{
br=new BufferedReader(new FileReader(fileName));//exception occurs here
// remaining business logic here
}
}
}
I am not sure how to solve this. One possible way is to use MyCtrl.class.getResourceAsStream(fileName)
But am not sure if that would work while still the application is getting deployed.