我的 groovy 程序创建了一个 csv,其中\n 表示新行。但客户在运行 Windows 时需要\r\n。
据我所知,这个问题在 SO 上得到了很多时间的回答,包括这里 How to remove line breaks from a file in Java?
此处的脚本不会将\n 替换为\r\n。\n 只是停留在
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);
}
最佳答案
您正在使用 readLine 读取文件中的一行 - 这已经删除了以您结尾的行。
您需要做的是像现在一样一次读取一行 - 然后在每行之间附加\r\n,而不是替换。
while ((line = reader.readLine()) != null) {
outData.append(line);
outData.append("\r\n");
}
关于java - 如何在groovy中将 "\n"替换为 "\r\n"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29824020/