Results 1 to 4 of 4
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Java File making new line

    Hi,

    I've got a method which has a String and writes it to a file. The problem I got is everytime I want to add a new String it puts the new String right next to the old one on the file where as I want to make each new String on a new line if its written. So for example this may be written with two seperate strings:

    text1text2

    But what I really want is the Strings on a new line:

    text1
    text2

    At my lesson they said at the end of the String if you add "\n" it makes a new line but it doesn't work for me. I'm on xp professional operating system.

    Please can someone help,

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    "\n" works when you're writing to the console or a Swing GUI text component, but to write to a file in an OS-dependent manner, you will need to get the current OS's line-separator String via:
    Java Code:
    String endOfLine = System.getProperty("line.separator");

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    Or use a PrintWriter instead of a FileWriter (saw that in your earlier thread) which has methods that end the line with a system dependent line separator.
    PrintWriter (Java Platform SE 6)

    db

  4. #4
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  2. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  3. delete a line from a text file in Java
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:08 AM
  4. Replies: 1
    Last Post: 11-10-2008, 09:49 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •