Java:如何在写入文件后正确关闭文件(我使用RandomAccessFile)
我的应用程序“加密”文件(业余)。。。我无法打开它处理的文件,当它通过以下代码时(直到我关闭应用程序),我如何才能正确释放该文件Java:如何在写入文件后正确关闭文件(我使用RandomAccessFile),java,file,Java,File,我的应用程序“加密”文件(业余)。。。我无法打开它处理的文件,当它通过以下代码时(直到我关闭应用程序),我如何才能正确释放该文件 SecureRandom random = new SecureRandom(); // file CAN be used RandomAccessFile raf = new RandomAccessFile(file, "rw"); // since RandomAccessFile, file can't be used.. it's ok FileChanne
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...
SecureRandom random=new SecureRandom();
//文件可以使用
RandomAccessFile raf=新的RandomAccessFile(文件“rw”);
//由于随机访问文件,无法使用该文件。。没关系
FileChannel=raf.getChannel();
MappedByteBuffer buffer=channel.map(FileChannel.MapMode.READ_WRITE,0,raf.length());
随机设定种子(键);
字节[]字节=新字节[(int)raf.length()];
读取(字节);
字节[]随机字节=新字节[(int)raf.length()];
random.nextBytes(randomBytes);
for(int i=0;i