gpt4 book ai didi

Android:为什么 BufferedReader 读取 EOF?

转载 作者:行者123 更新时间:2023-11-29 21:56:27 25 4
gpt4 key购买 nike

我正在模拟器中的 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 是如何创建的。

我关注了另一个关于如何通过给它一个错误的扩展名将文本文件解压缩到“ 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);

谢谢

最佳答案

根据评论,这是一段生产代码,删除了一些不相关的部分并保留了一些。如您所见,我们在 Assets 中有一些子目录。请注意,它使用方法 open 而不是 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;
}

关于Android:为什么 BufferedReader 读取 EOF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com