StringBuilder sometimes results in mangled lines











up vote
0
down vote

favorite












We have a Java application running which processes large amount of data and then writes it to CSV files. We have noticed that around 2% of the lines written are mangled and we have no idea why.



We use the following function for writing the lines:



static void writeLineToCSV(List<String> fields, Writer csvWriter) throws IOException {
StringBuilder line = new StringBuilder();

for (int i = 0; i < fields.size() - 1; i++) line.append(fields.get(i)).append(",");

line.append(fields.get(fields.size() - 1)).append("n");
csvWriter.write(line.toString());
csvWriter.flush();
}


Which results in lines which are mangled as follows:



2018-09-21T23:00:11.555Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:11.917Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:14592322,0.57377,-100,0.57644,1178.59788311,0.57375,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...


Notice how in the third line, for some reason, the first and second field are mangled together with the separating comma gone without a trace.



EDIT: for completion's sake, the schema of the CSV file is: Instant.toString(), timestamp (long), BigDecimal, ..., BigDecimal










share|improve this question




















  • 1




    Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
    – Konstantin Yovkov
    Nov 19 at 10:35








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    Nov 19 at 10:36






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    Nov 19 at 10:40






  • 1




    @user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
    – Michael
    Nov 19 at 10:41








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    Nov 19 at 10:55















up vote
0
down vote

favorite












We have a Java application running which processes large amount of data and then writes it to CSV files. We have noticed that around 2% of the lines written are mangled and we have no idea why.



We use the following function for writing the lines:



static void writeLineToCSV(List<String> fields, Writer csvWriter) throws IOException {
StringBuilder line = new StringBuilder();

for (int i = 0; i < fields.size() - 1; i++) line.append(fields.get(i)).append(",");

line.append(fields.get(fields.size() - 1)).append("n");
csvWriter.write(line.toString());
csvWriter.flush();
}


Which results in lines which are mangled as follows:



2018-09-21T23:00:11.555Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:11.917Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:14592322,0.57377,-100,0.57644,1178.59788311,0.57375,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...


Notice how in the third line, for some reason, the first and second field are mangled together with the separating comma gone without a trace.



EDIT: for completion's sake, the schema of the CSV file is: Instant.toString(), timestamp (long), BigDecimal, ..., BigDecimal










share|improve this question




















  • 1




    Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
    – Konstantin Yovkov
    Nov 19 at 10:35








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    Nov 19 at 10:36






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    Nov 19 at 10:40






  • 1




    @user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
    – Michael
    Nov 19 at 10:41








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    Nov 19 at 10:55













up vote
0
down vote

favorite









up vote
0
down vote

favorite











We have a Java application running which processes large amount of data and then writes it to CSV files. We have noticed that around 2% of the lines written are mangled and we have no idea why.



We use the following function for writing the lines:



static void writeLineToCSV(List<String> fields, Writer csvWriter) throws IOException {
StringBuilder line = new StringBuilder();

for (int i = 0; i < fields.size() - 1; i++) line.append(fields.get(i)).append(",");

line.append(fields.get(fields.size() - 1)).append("n");
csvWriter.write(line.toString());
csvWriter.flush();
}


Which results in lines which are mangled as follows:



2018-09-21T23:00:11.555Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:11.917Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:14592322,0.57377,-100,0.57644,1178.59788311,0.57375,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...


Notice how in the third line, for some reason, the first and second field are mangled together with the separating comma gone without a trace.



EDIT: for completion's sake, the schema of the CSV file is: Instant.toString(), timestamp (long), BigDecimal, ..., BigDecimal










share|improve this question















We have a Java application running which processes large amount of data and then writes it to CSV files. We have noticed that around 2% of the lines written are mangled and we have no idea why.



We use the following function for writing the lines:



static void writeLineToCSV(List<String> fields, Writer csvWriter) throws IOException {
StringBuilder line = new StringBuilder();

for (int i = 0; i < fields.size() - 1; i++) line.append(fields.get(i)).append(",");

line.append(fields.get(fields.size() - 1)).append("n");
csvWriter.write(line.toString());
csvWriter.flush();
}


Which results in lines which are mangled as follows:



2018-09-21T23:00:11.555Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:11.917Z,1537570811000,-7900,0.57642,39,0.57585,...
2018-09-21T23:00:14592322,0.57377,-100,0.57644,1178.59788311,0.57375,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...
2018-09-21T23:00:10.041Z,1537570810000,-4691.50311435,0.576,...


Notice how in the third line, for some reason, the first and second field are mangled together with the separating comma gone without a trace.



EDIT: for completion's sake, the schema of the CSV file is: Instant.toString(), timestamp (long), BigDecimal, ..., BigDecimal







java csv stringbuilder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 10:36

























asked Nov 19 at 10:32









user1870238

105315




105315








  • 1




    Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
    – Konstantin Yovkov
    Nov 19 at 10:35








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    Nov 19 at 10:36






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    Nov 19 at 10:40






  • 1




    @user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
    – Michael
    Nov 19 at 10:41








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    Nov 19 at 10:55














  • 1




    Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
    – Konstantin Yovkov
    Nov 19 at 10:35








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    Nov 19 at 10:36






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    Nov 19 at 10:40






  • 1




    @user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
    – Michael
    Nov 19 at 10:41








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    Nov 19 at 10:55








1




1




Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
– Konstantin Yovkov
Nov 19 at 10:35






Interesting, but how could we reproduce the issue? What does the List<String> contain? How do you open/print the resulting file?
– Konstantin Yovkov
Nov 19 at 10:35






1




1




Your List<String> fields is presumably wrong.
– Michael
Nov 19 at 10:36




Your List<String> fields is presumably wrong.
– Michael
Nov 19 at 10:36




2




2




It looks to me like multithreaded access gone wrong
– Milo Bem
Nov 19 at 10:40




It looks to me like multithreaded access gone wrong
– Milo Bem
Nov 19 at 10:40




1




1




@user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
– Michael
Nov 19 at 10:41






@user1870238 If the string that you enter pass the method has already concatenated the fields then there would be no separating comma. This is not magic. It can't just "not work". I don't even understand what fields you think are concatenated. Every line you've shown has six fields.
– Michael
Nov 19 at 10:41






2




2




Why do you think the problem is in the listed code rather than where List<String> fields is created ???
– Bruce Martin
Nov 19 at 10:55




Why do you think the problem is in the listed code rather than where List<String> fields is created ???
– Bruce Martin
Nov 19 at 10:55

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372680%2fstringbuilder-sometimes-results-in-mangled-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372680%2fstringbuilder-sometimes-results-in-mangled-lines%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin