Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何在写入文件后正确关闭文件(我使用RandomAccessFile)_Java_File - Fatal编程技术网

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