我越来越违反声纳:
“条件不应无条件地评估为“ TRUE”或“ FALSE””
对于下面的代码。
List<MediaContent> savedList = source.getChildMediaContents();
List<MediaContent> supplierList = target.getChildMediaContents();
// if existing and incoming both empty
if(savedList == null && supplierList == null){
return false;
}
// if one is null and other is not then update is required
if(savedList == null && supplierList != null){
return true;
}
if(savedList != null && supplierList == null){
return true;
}
Run Code Online (Sandbox Code Playgroud)
在两个if块下面,它给出错误
// if one is null and other is not then update is required
if(savedList == null …Run Code Online (Sandbox Code Playgroud) 声纳给出了一个错误,FileOutputStream应该将其关闭。我需要修改以下代码才能使用try-with-resources。我该怎么做呢?
public void archivingTheFile(String zipFile){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for(String file : this.fileList){
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
}catch(IOException ex){
LOGGER.error("Exception occurred while zipping file",ex);
}
}
Run Code Online (Sandbox Code Playgroud) 如何将以下代码转换为 JAVA 8 - try-with-resource。我用 java 6 编写了这段代码,我已将 java 6 升级到 java 8 - 声纳给出 Blocker 消息“应该使用 Try-with-resources”
public void archivingTheFile(String zipFile){
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for(String file : this.fileList){
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
zos.close();
}catch(IOException ex){
LOGGER.error("Exception occurred while zipping file",ex); …Run Code Online (Sandbox Code Playgroud)