0

我正在尝试读取一个大文件 chunk 。所以读操作将被多次调用,被偏移参数之一。读取工作正常。

但真正的问题是当我在读取完成后尝试删除文件时开始。它正在抛出 IO 异常。

我不想强制垃圾收集(System.gc()) 。

阅读代码:

public static GenericExcelRead ReadFileContent(String fileName, int offset, String status) throws IOException
{
    GenericExcelRead aGenericExcelRead = new GenericExcelRead();
    //FileInputStream fileStream = null;
    FileChannel fileChannel = null;
    MappedByteBuffer buffer;
    try(FileInputStream fileStream = new FileInputStream(fileName)) {

        fileChannel = fileStream.getChannel();
        buffer = null;
        if (status != "Completed")
        {
            if(fileChannel.size()>=(offset+1048756))
            {
                buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, offset, 1048756);
                aGenericExcelRead.setStatus("Partial");
                aGenericExcelRead.setEndOffset(offset+1048756);
            }
            else
            {
                buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, offset, (fileChannel.size()-offset));
                aGenericExcelRead.setStatus("Completed");
                aGenericExcelRead.setEndOffset((int)fileChannel.size());
            }       
            byte[] b = new byte[buffer.remaining()];
            buffer.get(b);
            String encodedcontent = new String(Base64.encodeBase64(b));
            buffer.clear();
            fileChannel.close();
            aGenericExcelRead.setData(encodedcontent);
            fileStream.close();
        }


    } catch (IOException e) {

        throw new IOException("IO Exception/File not found");
    }finally {

        if(fileChannel != null)
            fileChannel.close();
    }
    return aGenericExcelRead;
}
4

0 回答 0