Question

I was reading in a dictionary of words in my Android app in the emulator and caught the BufferedReader reading past the EOF. Why would it be doing that? Here is the code using the BufferedReader.

public void populateWordsTable(FileReader reader ) {
    try {
        BufferedReader br = new BufferedReader(reader);
        String s;
        while((s = br.readLine()) != null) {
            ContentValues cv=new ContentValues();
            String[] words = s.split(":");
            if ( words.length < 2 || words[1].isEmpty() || words[1].length() < 2 ) {
                System.out.println( "Skipping: " + s );
                continue; // only want words with lots of ending variations? to save time
            }
            String word  = words[0];
            System.out.println("adding: " + s);
            cv.put( WORD , word );
            db.insert( WORD_TABLE , null, cv);
        }
        br.close();
    }
    catch (IOException ex) {
        System.out.println( "Oh no, an exception: " + ex );
    }
    finally {
        reader.close();
    }
}

Here is the output showing it reading past the EOF for some reason:

note that "zyrian: zyrians," is the last line of the dictionary file I'm reading in from.

10-28 18:51:37.933: I/System.out(4473): adding: zygospore: zygospores, 
10-28 18:51:38.056: I/System.out(4473): adding: zygote: zygotes, 
10-28 18:51:38.155: I/System.out(4473): Skipping: zygotene: 
10-28 18:51:38.174: I/System.out(4473): Skipping: zygotic: 
10-28 18:51:38.174: I/System.out(4473): Skipping: zyloprim: 
10-28 18:51:38.174: I/System.out(4473): Skipping: zymase: 
10-28 18:51:38.174: I/System.out(4473): adding: zymogen: zymogens, 
10-28 18:51:38.284: I/System.out(4473): Skipping: zymoid: 
10-28 18:51:38.328: I/System.out(4473): Skipping: zymology: 
10-28 18:51:38.328: I/System.out(4473): Skipping: zymolysis: 
10-28 18:51:38.364: I/System.out(4473): Skipping: zymosis: 
10-28 18:51:38.364: I/System.out(4473): Skipping: zymotic: 
10-28 18:51:38.364: I/System.out(4473): Skipping: zymurgy: 
10-28 18:51:38.384: I/System.out(4473): adding: zyrian: zyrians, 
10-28 18:51:38.604: I/System.out(4473): Skipping: PK?????Y\A?????????????????????????????res/layout/activity_main.xml?TMkA~'??n??4??(x?T?????bE???,6?awb
10-28 18:51:38.604: I/System.out(4473): adding: ??P????????k?t?k?:????<????}?y??@??#??5???("??3??[??##???O????B!l??nx?Gl?-?J?O?A???8q?hG07????? ?;$
10-28 18:51:38.716: D/dalvikvm(4473): GC_CONCURRENT freed 396K, 55% free 2658K/5831K, external 929K/1038K, paused 8ms+7ms
10-28 18:51:38.788: I/System.out(4473): Skipping: #M??????tR(?è???n???I?',?ZI??mJ???o/]?,?q??u5? m??8\?F??4?Bh?f?[.?(E?}??U?0 6???????/??@?<!?b
10-28 18:51:38.788: I/System.out(4473): adding: q?+?R?#?{ ~?????S?g?2??u0??g????e"?????????:?t??'4m
10-28 18:51:38.866: I/System.out(4473): Skipping: M?I?Ss?kn#W?4?\?(i?????8??}*?P?5%?s??u8??!??5?9???7??!?Z?YKS??yd5?=[??d\i?W[f>X??|?r??f%M?????0??{?K?t??y???g3~E
10-28 18:51:38.866: I/System.out(4473): Skipping: ??SC?=,?????|???1?^Rh?9???]??ngg?_w??;?<_??\???sS?yE??L?x???;?~??byp7g8??

etc.

Thank you for any clues

Update:

speakingcode and full.stack.ex asked me to show how the FileReader got created.

I followed another thread about how to get a text file into the "assets" folder uncompressed by giving it a false extension. Like so...

 public void populateTables() {
      //TODO: is there a way to get the assets from the application, or does it have
  // to happen in an Activity?

      ThisVsThatApplication myapp = (ThisVsThatApplication)getApplicationContext();
      WordsDB db = myapp.getDB();
      if ( db == null ) {
        myapp.printerizer( "The database was not initialized.");
        return;
      }
      try {
        AssetFileDescriptor descriptor = getAssets().openFd("dictionary.jpg");
        FileReader reader = new FileReader(descriptor.getFileDescriptor());
            db.populateWordsTable(reader);
      } 
      catch ( IOException ex ) {
        System.out.println( "Oh noes an exception: " + ex );
      }
  }

so, maybe the FileReader thinks this is a binary file? I wonder if I can tell it it is a text file directly....

Using the code suggested by full.stack.ex, made this change here and it works!

InputStream is = getAssets().open("dictionary.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
db.populateWordsTable(br);

Thank you

Was it helpful?

Solution

Following up the comments, here's a piece of production code with some irrelevant parts removed and some kept. As you can see, we had some subdirectories in the assets. Please note that it uses method open rather than openFd. I may still be missing something, but this stuff has been working for quite a while.

public InputStream openInputStream(String inFileName) throws IOException {
    AssetManager assetManager = myContext.getAssets();
    String separator = "/";
    if ("".equals(getInputAssetSubdirectory())) {
        separator = "";
    }
    InputStream in = new BufferedInputStream(assetManager.open(getInputAssetSubdirectory() + separator + inFileName), BUFFER_SIZE);
    return in;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top