Write out a text file on Linux to be imported by Windows application, line break issues
<p>I've read a lot of stuff both on stackoverflow and around the net, I've also tried a dozen variations in my code &amp; am yet to find a solution.
The method has to write out a text file according to these file specs I was given, so that another application on a windows based system can import the file. Have discovered a test for this is that if the line breaks look correct in notepad then the import succeeds.</p>

<p>After the file is written to the server, it is then read again passed into a byte array that is later on attached to an email etc. (not sure it this would be relevant) </p>

<p>I started with the higher level classes I usually use to write out files, and as I read more on the subject, tried using FileOutputStream so I could specify the CRLF bytes explicitly. (Still doesn't align correctly in notepad which is the test I'm using)</p>

<pre><code>byte[] CRLF = { 0x0D, 0x0D, 0x0A };//"If you write \r\n on Windows, you'll actually get 0x0D 0x0D 0x0A in the file"
     Map&lt;String, ByteBuffer&gt; fileMap = new HashMap&lt;String, ByteBuffer&gt;();
     //eg: BA20110627-2.TXT
     Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
     DateFormat df = new SimpleDateFormat("yyyyMMdd");
     String date = df.format(nowTimestamp);
     int count = 1;
     for(Map&lt;String, List&lt;String&gt;&gt; fileLineListsAndTotAmountMap : fileLineListsAndTotAmountMaps){
         String fileName = "BA"+date+"-"+count+".TXT";
         FileOutputStream out = new FileOutputStream(fileStorePath+fileName);
         String header = ""+new Header(date).toString();
         byte[] headerAsBytes = header.getBytes();
         out.write(headerAsBytes);
         out.write(CRLF);
         //each map only has one entry
         Map.Entry&lt;String, List&lt;String&gt;&gt; entry = (Entry&lt;String, List&lt;String&gt;&gt;) fileLineListsAndTotAmountMap.entrySet().toArray()[0];
         for(String s: entry.getValue()){
                if (!s.equals("")) {
                    String line = s;
                    byte[] lineAsBytes = line.getBytes();
                    out.write(lineAsBytes);
                    out.write(CRLF);

                }
          }
         //totNumOfDebitRecords, totAmountForDebitRecords
         String footer = ""+new Footer(new Integer(fileLineListsAndTotAmountMap.size()).toString(), entry.getKey());
         byte[] footerAsBytes = footer.getBytes();
         out.write(footerAsBytes);
         out.write(CRLF);
         out.flush();
         out.close();
         fileMap.put(fileName, ByteBuffer.wrap(getBytesFromFile(new File(fileStorePath+fileName))));
      }
</code></pre>

<p>The only other thing I can think of trying is using maybe java.nio.charset.CharsetEncoder &amp; trying to encode it as ASCII though I can't really see how that would help.</p>

<p>Can anyone shed a little more light on this sort of issue?</p>
