我在android中创建CSV文件。文件正在成功创建,但如果内容中有新的行字符,则内容格式会受到干扰。 请建议如何处理换行符。
答案 0 :(得分:1)
如果可以提供有关如何接收和处理数据的其他详细信息,则可以提供有关如何将替换项放入方法的更详细示例。
您应该使用CSV文件中更易于处理的内容替换新行字符。我建议使用2个字符的字符串“\ n”,但您也可以将其替换为空白,破折号或完全删除该字符。
当你在它时,你可能还想处理换行符“\ r”和标签“\ t”。一定要逃避反斜杠字符。
示例(伪代码):
StringBuffer s = new StringBuffer();
char c;
loop-through-characters {
if(c == '\n') {
// Replace new-line with "\n".
s.append("\\n");
} else if(c == '\r') {
// Replace line-feed with "\r".
s.append("\\r");
} else if(c == '\t') {
// Replace tab with space " ".
s.append(" ");
} else {
// Otherwise, append the character as-is.
s.append(c);
}
}
// Process the bytes[] in StringBuffer s as you would have
// previously processed the raw bytes[].