0

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.

ADTC
  • 8,999
  • 5
  • 68
  • 93
asp008
  • 147
  • 1
  • 2
  • 12
  • Welcome to stack overflow. Please, try to keep your code smaller and to the point and to better describe your problem. – Anthony Accioly Jan 07 '14 at 12:40
  • One suggestion - when using FileOutputStream wrapped by BufferedOutputStream, it should be sufficient to call flush() and close() methods only for BufferedOutputStream instance. It will flush and close underlying FileOutputStream. – Jiri Kusa Jan 07 '14 at 12:52
  • This is actually mind-boggling, and deserves an upvote. Something must be amiss here. – ADTC Jan 07 '14 at 12:57
  • I have noticed that in your test code, you're using a `File` object in your `FileOutputStream` constructor. However, in your actual code, you're directly using the string path of the file. It shouldn't make a difference but, *would it?* – ADTC Jan 07 '14 at 13:00

2 Answers2

0

Try using:

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

instead of:

constituentContent.append(System.getProperty("line.separator"));
mauretto
  • 3,183
  • 3
  • 27
  • 28
0

If I understood your problem correctly I would replace the BufferedOutputStream with a OutputputStreamWriter + BufferedWriterspecifically setting the encoding. The last even have a handy newLine method:

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

Also, since you are not doing anything concurrently, I guess it is safe to replace StringBuffer with StringBuilder.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • Also take a look at [this question](http://stackoverflow.com/questions/9199216/strings-written-to-file-using-bufferedwriter) and its [answer](http://stackoverflow.com/a/9199636/664577) he is replacing `\n` with `newLine` when writting to the final file (in this way, inside the application he can work using only `\n`). – Anthony Accioly Jan 07 '14 at 12:58
  • Correction: _since you are **not*** doing anything concurrently_ – ADTC Jan 07 '14 at 13:02
  • Yeah, one word can make a big difference hehehe. Fixed, thank you. – Anthony Accioly Jan 07 '14 at 13:05
  • Is this the issue the asker is facing? I look at the code and I can't tell what's wrong with it. If his test code works, his main code should work too! – ADTC Jan 07 '14 at 13:07
  • 1
    His code is writing to the file the system using the default encoding. This one is making sure that it is written in UTF-8. I don't know if the linebreaks on his system are being set to `\n` or `\r\n`, if it is the first option fine, my code will work like a charm, if not he will have to use the strategy to replace line breaks suggested on my first comment. About why the main method is working and the other isn't, maybe on his original application the default encoding is being set somehow. See: http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding – Anthony Accioly Jan 07 '14 at 13:21
  • Also, we may be solving a problem that does not exist to begin with hehehe: http://stackoverflow.com/questions/8879277/textpad-and-unicode-full-support. OP, care to open your file using Kate / Gedit / Notepad++ or similar? – Anthony Accioly Jan 07 '14 at 13:38
  • Thanks for the reply.I tried using OutputStreamWriter but getting the same result. Not able to understand whats the issue here. DO you thing using a File object would really work. Please suggest. – asp008 Jan 07 '14 at 13:53
  • I am opening the file using Textpad. – asp008 Jan 07 '14 at 13:54
  • user, try to open it with a different editor. On your Linux machine use gedit or kate, on your windows machine use notepad++. Also try the suggestion on my first comment (building new linux when you write to the file). Cheers – Anthony Accioly Jan 07 '14 at 16:35