gpt4 book ai didi

android - BufferedInputStream 或 FileInputStream IOException

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:05 25 4
gpt4 key购买 nike

我刚刚发布了一个 Android 应用程序,它可以解析本地文件并对数据进行一些处理。几天前,我的一位客户向我报告了一个错误,每次他尝试处理他的文件时,应用程序都会崩溃。

这是他发给我的错误日志:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: lock == null
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:120)

与此相关的代码是这样的:

// EDIT 1: Following greenapps comment I will put a more realistic version of this 
// method (The first version was invented because I wanted to be breaf)
public void selectFile()
{
List<File> files = getDocsToParse();
this.listview.setAdapter(this.myadapter);
this.listview.setOnItemClickListener(new OnItemClickListener()
{
...
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
parseFile(files.get(position));
}
...
}
this.myadapter.addFiles(files);
}

public static List<File> getDocsToParse() {
File sdcard = Environment.getExternalStorageDirectory();
File subdir = new File(sdcard, "MyAppFolder/Files/");
// EDIT 2: I'm using subdir.mkdirs(); because I want to
// create MyAppFolder/Files/ folders the first time the user use the app.
// Is this not correct? Should I create these folders any other way?
if (!subdir.exists()) {
subdir.mkdirs();
}
File files[] = subdir.listFiles();
List<File> filterFiles = new ArrayList<File>();
for (int i = 0; i < files.length; i++) {
File file = files[i];
filterFiles.add(file);
}
return filterFiles;
}

public void parseFile(File fileToParse)
{
long totalSize = 0;
InputStream is = null;
try {
totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
try {
while ((line = reader.readLine()) != null) {
// Here I parse the file
}
} catch (IOException e) {
e.printStackTrace();
}
}

崩溃的是这一行:

reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

我知道这是因为“is”为 null,我必须捕获这种情况以避免应用程序崩溃(我将在下一个版本中修复此问题)。

编辑 3:你是对的,greenapps,我会在使用它之前检查它是否为空,否则我不会使用它。

我知道“is”是空的,因为有一个 IOException 这样做:

 totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));

编辑 4:当然,如果这给我一个 IOEXception,我将不得不更改我的代码以做出完全不同的事情。

而且我在 Internet 上找不到这两行代码可能引发 IOException 的原因。

我认为 fileToParse 没问题,或者至少不为空,因为我将这个"file"列表传递给适配器以使用 files.get(i).getName() 显示它们的文件名并且名称显示正确.

我必须补充一点,要处理的文件非常大并且包含敏感的个人数据,因此用户无法将其发送给我,因此我可以使用它进行测试。

我的文件都没有给我这个错误,如果没有有问题的文件,我很难追踪到问题,所以我不得不猜测。知道什么原因会导致此错误吗?

非常感谢和亲切的问候!

编辑 5:根据 ctarabusi 的建议,我将 parseFile 方法更改为:

public void parseFile(File fileToParse)
{
long totalSize = 0;
InputStream is = null;
try {
totalSize = fileToParse.length();
is = new BufferedInputStream(new FileInputStream(fileToParse));
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
StringTokenizer st = null;
while ((line = reader.readLine()) != null) {
// Here I parse the file
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error parsing file", Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if(is != null)
{
try
{
is.close();
}
catch (IOException e)
{
Log.e("", e.getMessage(), e);
}
}
}
}

我的测试再次正常运行,但用户告诉我他看到了“错误解析文件”消息,所以测试失败了。

我还能检查什么?

最佳答案

您的问题可能是您没有关闭输入流。文件描述符和流是有限的资源,使用完后释放它们很重要。

通常在 finally block 中完成,例如:

InputStream inputStream = null;
try
{
... use your input stream
}
catch (IOException e)
{
Log.e(TAG, e.getMessage(), e);
}
finally
{
if (inputStream != null)
{
try
{
inputStream.close();
}
catch (IOException e)
{
Log.e(TAG, e.getMessage(), e);
}
}
}

关于android - BufferedInputStream 或 FileInputStream IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570248/

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