【发布时间】:2012-10-28 19:19:18
【问题描述】:
我正在模拟器中阅读我的 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??
等等
感谢您提供任何线索
更新:
speakingcode 和 full.stack.ex 要求我展示 FileReader 是如何创建的。
我关注了另一个关于如何通过给它一个错误的扩展名来将文本文件解压缩到“资产”文件夹中的帖子。就这样……
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);
谢谢
【问题讨论】:
-
您确定文件末尾没有乱码吗?你也有代码显示你传递给方法的 FileReader 对象的实例化吗?
-
文件来自哪里?你是如何打开它来吸引读者的? adb pull 怎么样?
-
@speakingcode:是的,它只是一个文本文件,最后什么都没有。
-
@speakingcode:是的,我想您可能已经确定了问题所在。我关注了一个关于欺骗apk创建者通过给它一个错误的扩展名来允许我的文本文件解压缩的线程。所以我将文本文件命名为:dictionary.jpg 以将其解压缩到 apk 中。那么,也许 FileReader 将其视为二进制文件?我想知道我是否可以告诉它把它当作一个文本文件......
-
@full.stack.ex:我在上面的更新中添加了显示它来自哪里的代码......我会考虑 adb pull too thx
标签: android android-emulator bufferedreader eof garbage