Skip to content

Commit 1298924

Browse files
Updated FlatFileItemWriter to eliminate empty line at EOF
1 parent d6419f4 commit 1298924

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

spring-batch-core/src/test/resources/expectedOutput.ldif

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,4 @@ objectclass: person
7171
objectclass: organizationalPerson
7272
sn: Jensen
7373
cn: Horatio Jensen
74-
cn: Horatio N Jensen
75-
74+
cn: Horatio N Jensen

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.batch.item.file;
1818

19+
import java.util.Iterator;
1920
import java.util.List;
2021

2122
import org.springframework.batch.item.file.transform.LineAggregator;
@@ -39,6 +40,7 @@
3940
* @author Dave Syer
4041
* @author Michael Minella
4142
* @author Mahmoud Ben Hassine
43+
* @author Parikshit Dutta
4244
*/
4345
public class FlatFileItemWriter<T> extends AbstractFileItemWriter<T> {
4446

@@ -74,8 +76,16 @@ public void setLineAggregator(LineAggregator<T> lineAggregator) {
7476
@Override
7577
public String doWrite(List<? extends T> items) {
7678
StringBuilder lines = new StringBuilder();
77-
for (T item : items) {
78-
lines.append(this.lineAggregator.aggregate(item)).append(this.lineSeparator);
79+
Iterator<? extends T> iterator = items.iterator();
80+
if (hasExistingItems() && iterator.hasNext()) {
81+
lines.append(lineSeparator);
82+
}
83+
while (iterator.hasNext()) {
84+
T item = iterator.next();
85+
lines.append(this.lineAggregator.aggregate(item));
86+
if (iterator.hasNext()) {
87+
lines.append(lineSeparator);
88+
}
7989
}
8090
return lines.toString();
8191
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2018 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@
5959
* @author Dave Syer
6060
* @author Michael Minella
6161
* @author Mahmoud Ben Hassine
62+
* @author Parikshit Dutta
6263
*
6364
* @since 4.1
6465
*/
@@ -271,6 +272,7 @@ public void close() {
271272
if (state != null) {
272273
try {
273274
if (footerCallback != null && state.outputBufferedWriter != null) {
275+
state.outputBufferedWriter.write(lineSeparator);
274276
footerCallback.writeFooter(state.outputBufferedWriter);
275277
state.outputBufferedWriter.flush();
276278
}
@@ -325,7 +327,6 @@ private void doOpen(ExecutionContext executionContext) throws ItemStreamExceptio
325327
if (headerCallback != null) {
326328
try {
327329
headerCallback.writeHeader(outputState.outputBufferedWriter);
328-
outputState.write(lineSeparator);
329330
}
330331
catch (IOException e) {
331332
throw new ItemStreamException("Could not write headers. The file may be corrupt.", e);
@@ -359,6 +360,15 @@ public void update(ExecutionContext executionContext) {
359360
}
360361
}
361362

363+
protected boolean hasExistingItems() {
364+
try {
365+
return (state.position() > 0);
366+
}
367+
catch (IOException ioException) {
368+
throw new ItemStreamException("ItemStream is not able to read offset position");
369+
}
370+
}
371+
362372
// Returns object representing state.
363373
protected OutputState getOutputState() {
364374
if (state == null) {

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -296,7 +296,7 @@ public void testWriteRecordWithrecordSeparator() throws Exception {
296296
writer.open(executionContext);
297297
writer.write(Arrays.asList(new String[] { "1", "2" }));
298298
String lineFromFile = readLine();
299-
assertEquals("1|2|", lineFromFile);
299+
assertEquals("1|2", lineFromFile);
300300
}
301301

302302
@Test

0 commit comments

Comments
 (0)