getting extra CRLF with FileWriter

OS X Java 1.4

I am writing a string to a file with FileWriter.  The string has some CRLF's in it.  When the text is written, an extra CRLF is inserted wherever there is an existing one.  I don't want this.  I just want the original string written.

  public void writeData( String caseText )
   {
   //System.out.println ( "caseText is " + caseText );
     try
     {
       String strTargetFile = strWorkingDirectoryPath+ "Case&Questions.html";
       FileWriter fwOut = new FileWriter(strTargetFile);
       fwOut.write( caseText );
       fwOut.flush();
       fwOut.close();
     }catch(IOException e) { System.out.println( "IOException while attempting to write file" ); }
   }


Suggestions?

Thanks
je
jesterepsilonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
Depending on the origins of the String caseText, you may need to clean it of carriage returns for your Unix OS:

final String RE_CR = "\r";
caseText = caseText.replaceAll(RE_CR, "");
0
 
CEHJCommented:
That code will write *no* exta CRLFs so they  must all be in 'caseText'
0
 
CEHJCommented:
You can ensure there's only at most one per line with the following:


final String RE = "\n{2,}";
caseText = caseText.replaceAll(RE, "\n");
fwOut.write( caseText );
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DrWarezzCommented:
d'unno if this is right, but (assuming CEHJ is wrong; which I doubt), have you tried not using the .flush() method? Just stick with the .close() method?

gL,
[r.D]
0
 
CEHJCommented:
Now you mention flush, it's redundant: it should be removed. Removing it will not change the outcome though.
0
 
CEHJCommented:
>>you may need to clean it of carriage returns

And that should be done *before* the other replaceAll
0
 
jesterepsilonAuthor Commented:
This worked.  Thanks.  

final String RE_CR = "\r";
caseText = caseText.replaceAll(RE_CR, "");


Remaining question though:  Why did my System.out.println (as above, but uncommented) print to the terminal without  'extra spacing'?

Thanks
0
 
DrWarezzCommented:
:o\  That's a good question... CEHJ??
0
 
tdisessaCommented:
A better way to test your string is using this as you println statement:

System.out.println ( "caseText is [" + caseText  + "]");

That way you can look for extra spacing in between []s.
0
 
CEHJCommented:
>>Remaining question though:  

I suspect a heterogeneous treatment of line breaks. You could have a Windows line break implementation, (\r\n) but running on a Unix platform. Therefore to clean it of all \r first will help. The way this is manifested will differ between applications in which it's viewed.
0
All Courses

From novice to tech pro — start learning today.