Android:为什么BufferedReader读过EOF?

时间:2012-10-28 19:19:19

标签: android android-emulator bufferedreader eof garbage

我在模拟器中的Android应用程序中读了一个词典,然后抓住了BufferedReader读取EOF。为什么会这样做?这是使用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();
    }
}

以下输出显示它因某种原因读过EOF:

请注意,“zyrian:zyrians”是我正在阅读的字典文件的最后一行。

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??

感谢您提供任何线索

更新

speakcode和full.stack.ex让我展示了FileReader是如何创建的。

我跟着另一个关于如何通过赋予虚假扩展名将文本文件放入未压缩的“assets”文件夹的线程。像这样......

 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 );
      }
  }

那么,也许FileReader认为这是一个二进制文件?我想知道我是否可以直接告诉它它是一个文本文件....

使用full.stack.ex建议的代码,在此处进行此更改并且它可以正常工作!

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

谢谢

1 个答案:

答案 0 :(得分:2)

跟进评论,这里是一段生产代码,其中删除了一些不相关的部分,并保留了一些部分。如您所见,我们在资产中有一些子目录。请注意,它使用方法打开而不是 openFd 。我可能仍然遗漏了一些东西,但这些东西已经存在了很长一段时间。

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;
}