我想解析一個XML文件並用元素值填充字符串緩衝區。但是,如下所示,在讀取每個元素之後,我在StringBuffer
附加了一個新行字符。 XML文件從Windows機器複製並放在Unix服務器上的特定位置。新行字符沒有被寫入使用StringBuffer的文件
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不客氣。 – ADTC
一個建議 - 當使用由BufferedOutputStream包裝的FileOutputStream時,僅對BufferedOutputStream實例調用flush()和close()方法應該足夠了。它將刷新並關閉底層的FileOutputStream。 –