'Sonarqube how to fix not construct the path from user-controlled data

Below piece of code is giving vulnerability issue in sonarqueb "Change this code to not construct the path from user-controlled data"

BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file, false));

I tried below, but it is not working

if (file.exists() && file.isFile()) {
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file, false));
}

And

if(Files.exists(file.toPath()) && Files.isExecutable(file.toPath())) {
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file, false));
}

What i am missing to validate before creating new buffered stream?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source