Results 1 to 8 of 8
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default Writing to a .txt File

    I am new to Java and I had a question for a program I am writing. I want to save some data to a .txt file, and I got everything working other than one thing. I have no idea how to skip lines in the .txt file I tried /n as my friend suggested, but that didnt work. How should I go about doing this.
    Last edited by ngc0202; 07-26-2009 at 12:22 AM.

  2. #2
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Use:

    Java Code:
      System.getProperty("line.separator");
    As the line separator is different on different OS's

  3. #3
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Where do I put that in the code. I have:

    String text = "Text" + variable

    and then I need to make another on the .txt file to put more text.variables. Where would your code fit in?

    EDIT: I'm using Windows XP

  4. #4
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    In your example it would be:

    Java Code:
      String text = "Text" + System.getProperty("line.separator");
    ...or if your using it a lot do something like...

    Java Code:
      private final String NL = System.getProperty("line.separator");
      String text = "Text" + NL +
                          "MoreText" + NL;

  5. #5
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Thank you, that helps alot!

    EDIT: I tried your code out, using:
    private final String newLine = System.getProperty("line.separator");
    But when I compiled my code, I got an error message: "illegal start of expression" and it pointed to the line that I put your code at.
    Last edited by ngc0202; 07-26-2009 at 01:52 AM.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    you can also just use \n\r
    Different platforms will ignore the code that doesn't work. Unix/OS X use \n, windows \r generally.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Fix the error

    Quote Originally Posted by ngc0202 View Post
    Thank you, that helps alot!

    EDIT: I tried your code out, using:
    private final String newLine = System.getProperty("line.separator");
    But when I compiled my code, I got an error message: "illegal start of expression" and it pointed to the line that I put your code at.
    You may not want the private... try this instead
    Java Code:
    final String newLine = System.getProperty("line.separator");
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Sorry did not mention that, I usually put stuff like that as a class level constant for use by any of the methods. Just remove "private" as the preivious poster mentioned. :)

Similar Threads

  1. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  2. Writing to DAT or TXT file
    By hunterbdb in forum Advanced Java
    Replies: 7
    Last Post: 10-12-2008, 03:50 PM
  3. Replies: 9
    Last Post: 07-10-2008, 05:52 PM
  4. Writing to a file (at the end)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:22 AM
  5. writing to a file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 03:49 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
  •