不要硬编码'n' 和 'r'作为换行符

xiaoxiao2026-08-31  22

一、问题现象:    下面的代码主要用于将业务数据输出至文件中供接口调用: BufferedWriter out = null; try {    out = new BufferedWriter(new FileWriter(localFilePath + fileName, isAddLog));    ……    ……    if(isImepServerRunning)    {       out.write("004|" + hostName + "|" + ip + "|0002|imep进程up|0|" + performTime + “\r\n”);    }    else    {       out.write("004|" + hostName + "|" + ip + "|0002|imep进程down|1|" + performTime + “\r\n”);    } }    ……    此时出现了问题:结果文件中的数据换行在unix系统下不正确 二、解决方案: 1.由于unix和windows系统参数的差别,故不使用硬编码进行换行,通过使用System.getProperties("line.separator");获取换行符 2.通过使用out.newLine();实现 三、问题总结: 由该问题可延伸至操作系统常用参数的差别,如: line.separator(windows:"\r\n";unix:"\n"); file.separator(windows:"\";unix:"/"); path.separator(windows:";";unix:":"); 

转载请注明原文地址: https://www.6miu.com/read-5052166.html

最新回复(0)