Solved

getting extra CRLF with FileWriter

Posted on 2004-11-01
681 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
Question by:jesterepsilon
    10 Comments
     
    LVL 86

    Expert Comment

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

    Expert Comment

    by:CEHJ
    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
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    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:
    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
    >>you may need to clean it of carriage returns

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

    Author Comment

    by:jesterepsilon
    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
    :o\  That's a good question... CEHJ??
    0
     
    LVL 2

    Expert Comment

    by:tdisessa
    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
    >>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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now