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
    yesterday








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    yesterday






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    yesterday






  • 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
    yesterday








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    yesterday















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
    yesterday








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    yesterday






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    yesterday






  • 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
    yesterday








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    yesterday













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 yesterday

























asked yesterday









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
    yesterday








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    yesterday






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    yesterday






  • 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
    yesterday








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    yesterday














  • 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
    yesterday








  • 1




    Your List<String> fields is presumably wrong.
    – Michael
    yesterday






  • 2




    It looks to me like multithreaded access gone wrong
    – Milo Bem
    yesterday






  • 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
    yesterday








  • 2




    Why do you think the problem is in the listed code rather than where List<String> fields is created ???
    – Bruce Martin
    yesterday








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
yesterday






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






1




1




Your List<String> fields is presumably wrong.
– Michael
yesterday




Your List<String> fields is presumably wrong.
– Michael
yesterday




2




2




It looks to me like multithreaded access gone wrong
– Milo Bem
yesterday




It looks to me like multithreaded access gone wrong
– Milo Bem
yesterday




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
yesterday






@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
yesterday






2




2




Why do you think the problem is in the listed code rather than where List<String> fields is created ???
– Bruce Martin
yesterday




Why do you think the problem is in the listed code rather than where List<String> fields is created ???
– Bruce Martin
yesterday

















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



















































 


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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules