Results 1 to 6 of 6
  1. #1
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default 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
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Overwrite line in .txt file

    - read int from current file
    - alter value
    - create new file with new value

    Seems obvious enough. So what exactly don't you know how to do?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Overwrite line in .txt file

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    CruelCoin is offline Member
    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Default Re: Overwrite line in .txt file

    Quote Originally Posted by gimbal2 View Post
    So what exactly don't you know how to do?
    We're pretty new to file I/O.

    I could create a new file no problem, my issue is that i don't know how to create a new file with the same name.

    i.e:
    1: open count.txt with value 2000
    2: decrement by 1
    3: write new file called count.txt with value 1999

    I don't know how to do the third part.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Overwrite line in .txt file

    Quote Originally Posted by CruelCoin View Post
    I don't know how to do the third part.
    What have you tried?

    Recommended reading: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Overwrite line in .txt file

    Quote Originally Posted by CruelCoin View Post
    I could create a new file no problem, my issue is that i don't know how to create a new file with the same name.
    ??? There is no difference. If you create a new file while one already exists, the old one is overwritten.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  4. Replies: 3
    Last Post: 02-16-2009, 09:20 PM
  5. file overwrite method
    By nanaji in forum Advanced Java
    Replies: 5
    Last Post: 11-14-2008, 09: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
  •