0

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){
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Napmi
  • 521
  • 2
  • 13
  • 32
  • Did you try printing the exception stack trace using `ex.printStackTrace()`? – AbdullahC Oct 06 '13 at 14:51
  • Error posted! thanks for reminding me about log cat – Napmi Oct 06 '13 at 14:55
  • Rather than use a full pathname, use AssetManager to access the file. See this question: http://stackoverflow.com/questions/9544737/read-file-from-assets – EJK Oct 06 '13 at 15:02
  • I just used the assetmanager to make one of this AssetManager assetManager = getAssets(); however i get an error on the getAssets Cannot make a static reference to the non-static method getAssets() from the type ContextWrapper – Napmi Oct 06 '13 at 15:23
  • Ok i found my solution . :/ however i lack the reputation to actually post my answer ..... – Napmi Oct 06 '13 at 16:01
  • Can you edit your question to show your solution? – jboi Oct 06 '13 at 16:53

0 Answers0