diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java index cf04d21eb..22ddbdd29 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -270,7 +270,9 @@ public abstract class AbstractFileItemWriter extends AbstractItemStreamItemWr state.close(); if (state.linesWritten == 0 && shouldDeleteIfEmpty) { try { - resource.getFile().delete(); + if (!resource.getFile().delete()) { + throw new ItemStreamException("Failed to delete empty file on close"); + } } catch (IOException e) { throw new ItemStreamException("Failed to delete empty file on close", e); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 8af557693..fd7283f1f 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -735,7 +735,9 @@ public class StaxEventItemWriter extends AbstractItemStreamItemWriter } if (currentRecordCount == 0 && shouldDeleteIfEmpty) { try { - resource.getFile().delete(); + if (!resource.getFile().delete()) { + throw new ItemStreamException("Failed to delete empty file on close"); + } } catch (IOException e) { throw new ItemStreamException("Failed to delete empty file on close", e);