-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FlatFileItemWriter creates empy line at eof. Could we allow option to disable? #3863
Comments
Hi @benas, The following test from @Test
public void testWriteRecordWithrecordSeparator() throws Exception {
writer.setLineSeparator("|");
writer.open(executionContext);
writer.write(Arrays.asList(new String[] { "1", "2" }));
String lineFromFile = readLine();
assertEquals("1|2|", lineFromFile);
} I can work on it, if you and team find value. |
I don't see an obvious way to detect that we are writing the last chunk to not add a line separator at the end. I'm probably missing a clever way to do that, but even if we try to invert the code to something like this: @Override
public String doWrite(List<? extends T> items) {
StringBuilder lines = new StringBuilder();
for (T item : items) {
//BECAUSE OF THIS LOOP, THE LINE SEPARATOR IS ALWAYS ADDED, ALSO ON LAST LINE
-- lines.append(this.lineAggregator.aggregate(item)).append(this.lineSeparator);
++ lines.append(this.lineSeparator).append(this.lineAggregator.aggregate(item));
}
return lines.toString();
} the problem remains, but at the other end of the file (ie the head/beginning of the file). @Jenson3210 @parikshitdutta Do you agree? |
As you might have guessed based on my limited description, I do not have that much experience with Spring Batch. But I thought it was still valid to have this option though. If to hard to implement, there are options to handle this behaviour in consuming steps too... @benas Am I correct to understand that it would be too hard because this one piece can be called in chuncks, and thus passed multiple times? I assume this because if not passing multiple times, we could fix it without an enhanced for-loop, as you most certainly are aware of 😃 |
My question is more about the feasibility of the feature rather than its added value. In order to add (or not) a line separator at the end of the file, Spring Batch needs to know if the current chunk to write is the last one (ie know the total number of items upfront). This information is not available (unless we ask the user to provide it in some way, but this is another story) which makes me wonder if this is even possible in the first place. Probably there is a way to invert the current behaviour to not add a line separator by default and use a footer callback that adds a final separator at the end, but I'm not sure if this would work or if it would be backward compatible. I'm open to review a PR if there is a way (that I'm missing) to implement this feature. |
@benas, I agree this is more about feasibility. Probably by updating As far as using The other could be by introducing callback interfaces, but I believe that would be over engineering for something that can be handled in the client code. |
The
The difficulty to address this is due to the way items are written according to the chunk-oriented processing model. Even without an enhanced loop, I believe the problem will remain in one of the ends of the file (and in between chunks). Here is a repo where I tried this approach. Based on the previous comments, I'm closing this issue for now. But again, I'm open to review a PR that could implement this feature in a backward compatible way. |
Expected Behavior
To have an option in the FlatFileItemWriterBuilder that allows you to disable the empty line at eof.
.noEmptyLineAtEndOfFile()
in the example.could result in a file that has no last empty line by altering the writer
doWrite
method.Current Behavior
The for loop appends this.lineseparator at end of String that will be written to file.
Context
The text was updated successfully, but these errors were encountered: