文件通过socket传输后 想要删除本地的对应文件
通过File delete 失败, 在程序未关闭情况下 手动删除,提示 文件正在被使用,按理解来说,这应该是文件流没有关闭的情况,但是,我已经写了close 了,而且经过测试,在不改动代码的情况下,如果不经过socket传输, 文件是可以删除的。
但是经由socket以后,文件就无法删除,我也试了 deleteOnExit 代码if 测试 提示,删除成功,但是 查看文件夹 后 文件并没有被删除。
何解? Help ~!!
private void TransFileToServer(String EnFilePath,OutputStream out) throws Exception{
FileInputStream fileIn= new FileInputStream(EnFilePath);
FileChannel fcIn=null;
MappedByteBuffer mbbfIn=null;
int len=0,outLenth;
fcIn=fileIn.getChannel();
mbbfIn=fcIn.map(FileChannel.MapMode.READ_ONLY, 0, fcIn.size());
byte[] outBytes=new byte[1024];
boolean more=true;
while(more){
len=mbbfIn.limit()-mbbfIn.position();
if(len>outBytes.length)
{ mbbfIn.get(outBytes, 0, outBytes.length);
out.write(outBytes);
}else{
more=false;
}
}
if(len>0){mbbfIn.get(outBytes, 0, len);
out.write(outBytes,0,len);
}
if(fcIn!=null)
fcIn.close();
if(fileIn!=null)
fileIn.close();
if(out!=null)
out.close();
if(clientSocket!=null)
clientSocket.close();
}
代码正常的。 就是无法删除。 这些是放在一个 swingworker 线程里,也执行完毕了