我正在模拟器中阅读我的 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 是如何创建的。
我关注了另一个关于如何通过给它一个错误的扩展名将文本文件解压缩到“资产”文件夹中的线程。像这样...
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);
谢谢