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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CEHJCommented:
Now you mention flush, it's redundant: it should be removed. Removing it will not change the outcome though.
0
CEHJCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.