【发布时间】:2014-01-25 03:28:47
【问题描述】:
我正在尝试解析 XML 文件并使用元素值填充字符串缓冲区。但是,在读取每个元素后,我将在StringBuffer 中添加一个换行符,如下所示。 XML 文件是从 Windows 机器上复制的,并放在 Unix 服务器上的特定位置。
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("Constituent") && isEquityIndexPositionFile) {
constituentContent.append(constCurrValue);
constituentContent.append(COLUMN_SEPARATOR);
constituentContent.append(constCurrName);
constituentContent.append(COLUMN_SEPARATOR);
constituentContent.append("");// mul_factor
constituentContent.append(COLUMN_SEPARATOR);
constituentContent.append("");// weight
constituentContent.append(COLUMN_SEPARATOR);
constituentContent.append(businessDate);
constituentContent.append(COLUMN_SEPARATOR);
constituentContent.append(currDateString);
constituentContent.append(System.getProperty("line.separator"));
请假设以上所有值都来自 XML 文件。这里COLUMN_SEPARATOR 是|。
现在我想将StringBuffer constituentContent 的内容写入如下文件:
public void endDocument() throws SAXException {
try {
if(constituentContent.toString().length() > 0)
{
System.out.println(constituentContent.toString());
fos = new FileOutputStream(RAW_DATA_FILE_LOC + CONSTITUENT_FILE, true);
bos = new BufferedOutputStream(fos);
bos.write(constituentContent.toString().getBytes());
fos.flush();
bos.flush();
fos.close();
bos.close();
log.info("created the index constituent .dat file");
}
} catch (Exception e) {
log.info("exception while creating the index constituent .dat file");
e.printStackTrace();
}
}
System.out.println 语句使用行分隔符在控制台上打印内容,但是当我在 TextPad 中打开文件时,所有值都在一行中。我已经尝试了行分隔符的所有选项\n,\r\n,但无济于事。问题出在 Unix 操作系统上。
我还创建了这个示例程序并在 Unix 上运行。
public class Demo {
public static void main(String[] args) throws IOException {
final String seperator = System.getProperty("file.separator");
final String lineSeperator = System.getProperty("line.separator");
System.out.println("File Separator is"+seperator );
System.out.println("Line Separator is"+ " "+lineSeperator);
StringBuffer headerContent=new StringBuffer();
headerContent.append("1026564");
headerContent.append("|");
headerContent.append("1005503");
headerContent.append("|");
headerContent.append("391.6000");
headerContent.append("|");
headerContent.append("INR");
headerContent.append("|");
headerContent.append("|");
headerContent.append("|");
headerContent.append("2013-12-03");
headerContent.append("|");
headerContent.append("|");
headerContent.append("06 Jan 2014");
headerContent.append(lineSeperator);
headerContent.append("1026564");
headerContent.append("|");
headerContent.append("1005503");
headerContent.append("|");
headerContent.append("391.6000");
headerContent.append("|");
headerContent.append("INR");
headerContent.append("|");
headerContent.append("|");
headerContent.append("|");
headerContent.append("2013-12-03");
headerContent.append("|");
headerContent.append("|");
headerContent.append("06 Jan 2014");
headerContent.append(lineSeperator);
System.out.println(headerContent.toString());
File file=new File("/opt/compliance/Atish.dat");
FileOutputStream out= new FileOutputStream(file,true);
BufferedOutputStream bos = new BufferedOutputStream(out);
bos.write(headerContent.toString().getBytes());
bos.flush();
out.close();
bos.close();
}
}
在这里它可以工作,写入文件的数据不在一行中。行分隔符在这里工作。请建议。
【问题讨论】:
-
欢迎堆栈溢出。请尽量让您的代码更小、更切题,并更好地描述您的问题。
-
@user3164538 不客气。
-
一个建议 - 当使用由 BufferedOutputStream 包装的 FileOutputStream 时,只为 BufferedOutputStream 实例调用 flush() 和 close() 方法就足够了。它将刷新并关闭底层 FileOutputStream。
-
这实际上令人难以置信,值得一票。这里一定有问题。
-
我注意到在您的测试代码中,您在
FileOutputStream构造函数中使用了File对象。但是,在您的实际代码中,您直接使用文件的字符串路径。它不应该有所作为,但是,会吗?
标签: java