Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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 “如何避免声纳”;“资源应关闭”;拦截器警告_Java_Sonarqube - Fatal编程技术网

Java “如何避免声纳”;“资源应关闭”;拦截器警告

Java “如何避免声纳”;“资源应关闭”;拦截器警告,java,sonarqube,Java,Sonarqube,我不想立即关闭RandomAccessFile,但它会在声纳扫描时警告“资源应该关闭”。如何避免这种情况?示例代码如下所示: public class AccessFileTest { private static RandomAccessFile accessFile; private static FileChannel fileChannel; private static long maxFileLength = 1024 * 1024; private

我不想立即关闭RandomAccessFile,但它会在声纳扫描时警告“资源应该关闭”。如何避免这种情况?示例代码如下所示:

public class AccessFileTest {
    private static RandomAccessFile accessFile;
    private static FileChannel fileChannel;
    private static long maxFileLength = 1024 * 1024;
    private static long currentFileLenth = 0;

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            write("test");
        }
    }

    private static void write(String text) {
        try {
            if (accessFile == null) {
                accessFile = new RandomAccessFile("", "rw");
                fileChannel = accessFile.getChannel();
            }
            ByteBuffer buffer = ByteBuffer.wrap(text.getBytes());
            fileChannel.write(buffer);
            closeAccessFileIfNeed(text.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void closeAccessFileIfNeed(byte[] bytes) throws IOException {
        currentFileLenth += bytes.length;
        if (currentFileLenth >= maxFileLength) {
            accessFile.close();
        }
    }
}
公共类AccessFileTest{
私有静态随机存取文件;
专用静态文件通道文件通道;
私有静态长maxFileLength=1024*1024;
私有静态长currentFileLenth=0;
公共静态void main(字符串[]args){
对于(int i=0;i<1000;i++){
写入(“测试”);
}
}
私有静态无效写入(字符串文本){
试一试{
if(accessFile==null){
accessFile=新的随机访问文件(“,“rw”);
fileChannel=accessFile.getChannel();
}
ByteBuffer buffer=ByteBuffer.wrap(text.getBytes());
写入(缓冲区);
closeAccessFileIfNeed(text.getBytes());
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有静态void closeAccessFileIfNeed(字节[]字节)引发IOException{
currentFileLenth+=字节数.length;
如果(currentFileLenth>=maxFileLength){
accessFile.close();
}
}
}

您可以在“资源试用”中尝试此操作吗

在这里

当您使用try with resource时,jVM会自动调用close(),您不需要手动执行任何操作

如果不使用try with resource,Sonar会说
应该关闭资源
意思是,您应该手动关闭它,在finally块内

 try (RandomAccessFile accessFile = new RandomAccessFile("", "rw")) {

      FileChannel fileChannel = accessFile.getChannel();
    } catch (IOException e) {

    }


@鸟人,我想,这将是答案,如果不是添加我的注释来解释,谢谢,我的意思是我不想在这个块中立即关闭它,有一些特殊的逻辑稍后关闭它,例如,当我调用方法一次一次地写一个文件时,希望在文件达到固定长度后关闭该文件。有一些避免声纳扫描的技巧吗?你运行了吗?你能运行这个模块并让我知道吗?实际上,在你的问题中,提到了
如何避免声纳“资源应该关闭”拦截器警告,我已经给出了解释以及如何解决,如果可以,你可以接受它