OutputStream给出"关闭失败:EIO(I / O错误)"删除文件运行时

时间:2014-05-29 11:41:26

标签: android android-asynctask inputstream outputstream

当我删除当前正在写入模式的文件时出现错误Error : close failed: EIO (I/O error)

实际上我正在使用AsyncTask下载文件,我可以在两者之间取消此下载。 为了停止这个下载我正在删除该文件并连续检查文件存在与否的写入数据循环,如果找不到文件,那么我正在打破该循环并返回onPostExecute但是它抛出错误。 / p>

那么如何删除该错误并正确使用output.flush()output.close()

MyCode是:

@Override
protected String doInBackground(Void... params) {

    int count;
    try {
    URL url = new URL(taskHolder.fileUrl);
    URLConnection connection = url.openConnection();
    connection.connect();

    InputStream input = new BufferedInputStream(url.openStream());

    outputDir = PROCESSING_PATH + taskHolder.fileName;
    OutputStream output = new FileOutputStream(outputDir);

    byte data[] = new byte[1024*4];

    long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        output.write(data, 0, count);

        /**
         * When user cancelling downloads, 
         * file will be deleted from that 
         * folder so here have to check 
         * file is exists or not.
         */
        if(!(new File(outputDir).exists())) {
        returnValue = "cancelled";
        break;
        }

    }

    returnValue = "1";
    output.flush();
    returnValue = "2";
    output.close();
    returnValue = "3";
    input.close();
    returnValue = "4";

    } catch (Exception e) {
    Log.e(TAG, "Error : "+e.getLocalizedMessage() + " : " + returnValue);
    returnValue = null;
    }

    return returnValue;
}

0 个答案:

没有答案