This might be a duplicated thread however after trying out several and multiple threads my problem still continues :/
I have this method which reads a dictionary and does decryption with it . However, I have no idea how to get it to read the dictionary. I drag and dropped my dictionary to the assets folder and then used this code.
try {
System.out.println("Dictionary Initialize");
BufferedReader inputReader;
FileInputStream fis = new FileInputStream("/assets/dictionary-english.txt");
BufferedReader dictionary = new BufferedReader(new InputStreamReader(fis));
String checktext;
while ((checktext = dictionary.readLine()) != null) {
dictionaryset.add(checktext);
}
System.out.println("Dictionary Successfully Initialized");
} catch (IOException ex) {
System.out.println("Error! reading!");
}
After several test, I have confirmed that it could not even reach the while loop as the try and catch catches the error. It also caught an IOException
error. Any idea how to fix this? Thanks
This is my error from the logcat
10-06 14:54:50.555: W/System.err(1980): java.io.FileNotFoundException: /assets/dictionary-english.txt (No such file or directory)
10-06 14:54:50.555: W/System.err(1980): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
10-06 14:54:50.555: W/System.err(1980): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
10-06 14:54:50.555: W/System.err(1980): at java.io.FileInputStream.<init>(FileInputStream.java:80)
10-06 14:54:50.565: W/System.err(1980): at java.io.FileInputStream.<init>(FileInputStream.java:132)
Edited: Tried using
AssetManager assetManager = getAssets();
However I get an error on the getAssets()
which is "Cannot make a static reference to the non-static method getAssets() from the type ContextWrapper"
========================================================================
Answer:
BufferedReader dictionary = new BufferedReader(new InputStreamReader(getAssets().open("dictionary-english")));
I used this line of code to get my file and in the method I removed the static
which solved my cannot make a static reference problem
eg; private static String run1(String cipherText){ --> private String run1(String cipherText){