I am trying to parse an XML file and populating a string buffer with element values. However, I am appending a new line character to StringBuffer
after reading each element as follows. XML file is copied from Windows machine and put in specific location on Unix server.
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"));
Please assume all above values are coming from XML file. here COLUMN_SEPARATOR
is |
.
Now I want to write the contents of the StringBuffer
constituentContent
to a file like this:
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
statements prints the contents on console with line seperator but when I open the file in TextPad all values are in a single line. I have tried all the options for line separator as \n
,\r\n
but to no avail. Problem is on Unix OS.
Also I have created this sample program and ran on 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();
}
}
Here it works and data written to a file is not in a single line. Line seperator works here. Please suggest.