Question:
In a simple Java application, everything is simple. Used by BufferReader.
try {
BufferedReader in = new BufferedReader(new FileReader("dictionary.txt"));
while ((word = in.readLine()) != null) {
dictionary.put(word, 0);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
And then all the lines of the file are written into the HashMap, with which we work further.
But how to make Android see this file. The file is located in the asset folder. It would seem logical that:
try {
AssetManager assetManager;
BufferedReader in = new BufferedReader(assetManager.open("dictionary.txt"));
while ((word = in.readLine()) != null) {
dictionary.put(word, 0);
}
in.close();
//assetManager.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
But no, a mistake. How do I get this file?
UPD * SOLVED * Method:
public static void loadDictionary (Context context) throws IOException {
try {
AssetManager assetManager = context.getAssets();
InputStreamReader istream = new InputStreamReader(assetManager.open("dictionary.txt"));
BufferedReader in = new BufferedReader(istream);
while ((word = in.readLine()) != null) {
dictionary.put(word, 0);
}
in.close();
} catch (FileNotFoundException e) {
// FileNotFoundExpeption
} catch (IOException e) {
// IOExeption
}
}
Call:
try {
Spell.loadDictionary(getBaseContext());
} catch (IOException e) {
//IOExpeptino
}
Спасибо user – afiki
Answer:
AssetManager assetManager = this.getAssets();
and you are trying to call a method on null.
try {
AssetManager assetManager = this.getAssets();
InputStreamReader istream = new InputStreamReader(assetManager.open("dictionary.txt"));
BufferedReader in = new BufferedReader(istream);
while ((word = in.readLine()) != null) {
dictionary.put(word, 0);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}