Reputation: 4202
My groovy program creates a csv that is has \n for new line. But the customer wants \r\n as they run windows.
I understand that this question is answered a tonne of time on SO including here How to remove line breaks from a file in Java?
This script here does not replace \n with \r\n. The \n just stays on
newline = System.getProperty("line.separator");
for (int i = 0; i < dataContext.getDataCount(); i++) {
InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);
reader = new BufferedReader(new InputStreamReader(is));
outData = new StringBuffer();
lineNum = 0;
while ((line = reader.readLine()) != null) {
line.replace(newline, "\r\n")
outData.append(line);
}
is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
dataContext.storeStream(is, props);
}
Upvotes: 0
Views: 6270
Reputation: 41210
You are reading the file a line at the time by using readLine - this already removes the line ending for you.
What you need to do is read the lines one at a time as you do now - then append \r\n in between each line, not replace.
while ((line = reader.readLine()) != null) {
outData.append(line);
outData.append("\r\n");
}
Upvotes: 3