diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java index 7804b14..3621329 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/service/OpenTofuDirectoryService.java @@ -321,22 +321,29 @@ private Map getImportantFilesContent(String workspace) { if (workPath.isDirectory() && workPath.exists()) { File[] files = workPath.listFiles(); if (Objects.nonNull(files)) { - List importantFiles = Arrays.stream(files) - .filter(file -> file.isFile() && !isExcludedFile(file.getName())).toList(); - for (File importantFile : importantFiles) { - try { - String content = Files.readString(importantFile.toPath()); - fileContentMap.put(importantFile.getName(), content); - } catch (IOException e) { - log.error("Read content of file with name:{} error.", - importantFile.getName(), e); - } - } + Arrays.stream(files).filter(file -> file.isFile() + && !isExcludedFile(file.getName())).forEach(file -> { + String content = readFileContentAndDelete(file); + fileContentMap.put(file.getName(), content); + }); } } return fileContentMap; } + private String readFileContentAndDelete(File file) { + String fileContent = ""; + try { + fileContent = Files.readString(file.toPath()); + boolean deleted = Files.deleteIfExists(file.toPath()); + log.info("Read file content with name:{} successfully. Delete result:{}", + file.getName(), deleted); + } catch (IOException e) { + log.error("Read file content with name:{} error.", file.getName(), e); + } + return fileContent; + } + private void deleteWorkspace(String workspace) { Path path = Paths.get(workspace); try (Stream pathStream = Files.walk(path)) {