我有以下代码,但使用findbugs失败。
protected void writeStream(final InputStream inputStream, final Path destinationFile) throws IOException {
final Path parentDirectory = destinationFile.getParent();
Path tempFile = null;
try {
// Create the temporary dir for temporary download
Files.createDirectories(getTempDownloadPath());
Files.createDirectories(parentDirectory);
// Create the temporary file in the temporary dir
tempFile = Files.createTempFile(getTempDownloadPath(), destinationFile.getFileName().toString(), ".tmp");
long t1 = System.currentTimeMillis();
Files.copy(inputStream, tempFile, REPLACE_EXISTING);
long t2 = System.currentTimeMillis();
Files.move(tempFile, destinationFile, ATOMIC_MOVE, REPLACE_EXISTING);
long t3 = System.currentTimeMillis();
} catch (final IOException e) {
log.error("Failed to write file.", e);
throw e;
} finally {
IOUtils.closeQuietly(inputStream);
try {
if (tempFile != null) {
Files.deleteIfExists(tempFile);
}
} catch (IOException e) {
log.warn("Failed to delete file: " + tempFile, e);
}
}
}
它在抱怨
tempFile = Files.createTempFile(getTempDownloadPath(), destinationFile.getFileName().toString(), ".tmp");
由于调用方法的返回值,可能导致空指针取消引用
哪一部分是错误的,我该如何解决?
答案 0 :(得分:0)
Files.createTempFile不会返回Path对象,而是一个字符串。