我的应用程序“加密”文件(业余)......当它通过以下代码时,我无法打开它处理的文件(直到我关闭应用程序)如何正确释放文件?
SecureRandom random = new SecureRandom();
// file CAN be used
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// since RandomAccessFile, file can't be used.. it's ok
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
random.setSeed(key);
byte[] bytes = new byte[(int) raf.length()];
raf.read(bytes);
byte[] randomBytes = new byte[(int) raf.length()];
random.nextBytes(randomBytes);
for (int i = 0; i < bytes.length; i++)
buffer.put((byte) (bytes[i] + randomBytes[i]));
buffer.force();
channel.close();
raf.close();
// fails to free the file...