2014-01-07 37 views
0

我想解析一個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|

現在我想的StringBufferconstituentContent的內容寫到這樣的文件:

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(); 
    } 
} 

這裏它的工作原理和寫入文件的數據不在一行中。線分離器在這裏工作。請建議。

+0

歡迎來到堆棧溢出。請儘量保持您的代碼小一點,並更好地描述您的問題。 –

+0

@ user3164538不客氣。 – ADTC

+0

一個建議 - 當使用由BufferedOutputStream包裝的FileOutputStream時,僅對BufferedOutputStream實例調用flush()和close()方法應該足夠了。它將刷新並關閉底層的FileOutputStream。 –

回答

0

嘗試使用:

constituentContent.append("\r\n"); 

代替:

constituentContent.append(System.getProperty("line.separator")); 
+0

他似乎已經嘗試了所有這些......他的所有演講都在代碼中丟失了! – ADTC

0

如果我明白你的問題正確我將與OutputputStreamWriter + BufferedWriter專門設置編碼代替BufferedOutputStream。最後甚至有一個方便的方法newLine

OutputStreamWriter ost = new OutputStreamWriter(
    new FileOutputStream("/opt/compliance/Atish.dat", true), 
    Charset.forName("UTF-8").newEncoder() 
); 
BufferedWriter bw = new BufferedWriter(ost); 
bw.write(constituentContent.toString()); 

而且,由於你沒有做任何事情的同時,我想這是安全與StringBuilder更換StringBuffer

+0

也看看[這個問題](http://stackoverflow.com/questions/9199216/strings-written-to-file-using-bufferedwriter)和它的[答案](http://stackoverflow.com/a/9199636/664577)當寫入最終文件時,他正在用'newLine'替換'\ n'(這樣,在應用程序內部,他只能使用'\ n')。 –

+0

更正:_因爲你是**不***兼任何事_ – ADTC

+0

是的,一個詞可以使一個很大的差異hehehe。修正了,謝謝。 –