Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: (Java) Overwrite line in .txt file

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (Java) Overwrite line in .txt file

    Hi All.

    I'm trying to create a counter that will survive the program being closed and reopened.

    I need to start with a txt file that has one line in it with 2000 as the text

    On running the program, i need this to be overwritten with 1999.
    Running again will overwrite with 1998, etc etc.

    Any tips on how i could do this?

    CC.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: (Java) Overwrite line in .txt file

    Quote Originally Posted by CruelCoin View Post
    I'm trying to create a counter that will survive the program being closed and reopened.

    I need to start with a txt file that has one line in it with 2000 as the text

    On running the program, i need this to be overwritten with 1999.
    Running again will overwrite with 1998, etc etc.

    Any tips on how i could do this?
    Updating a file, in the sense to keep the actual content and "pass" over changing only some things is more difficult, especially if it's a text file.
    In your case, it's sufficient to recreate the file using any of the standard I/O classes for output.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (Java) Overwrite line in .txt file

    Hi,

    Am i correct then in saying that using Filewriter with the same filename will overwrite the existing file? Or would it append?

    CC.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: (Java) Overwrite line in .txt file

    Quote Originally Posted by CruelCoin View Post
    Am i correct then in saying that using Filewriter with the same filename will overwrite the existing file? Or would it append?
    FileWriter overwrites (in other words: rewrite from the beginning) the file .... unless you use the constructor with the boolean append parameter so that you can choose.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: (Java) Overwrite line in .txt file

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/84305-overwrite-line-txt-file.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: November 13th, 2012, 06:02 AM
  2. Program to read .TXT file one line at a time
    By JustForLawls in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2012, 02:02 PM
  3. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM