Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getting extra CRLF with FileWriter

Posted on 2004-11-01
12
Medium Priority
?
700 Views
Last Modified: 2008-02-20
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
0
Comment
Question by:jesterepsilon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12465967
That code will write *no* exta CRLFs so they  must all be in 'caseText'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12466147
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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12466152
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12466189
Now you mention flush, it's redundant: it should be removed. Removing it will not change the outcome though.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12466285
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12466289
>>you may need to clean it of carriage returns

And that should be done *before* the other replaceAll
0
 

Author Comment

by:jesterepsilon
ID: 12467082
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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12467166
:o\  That's a good question... CEHJ??
0
 
LVL 2

Expert Comment

by:tdisessa
ID: 12467751
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12468556
>>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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question