Results 1 to 6 of 6
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default Inserting line into text file

    Hello,

    I want to insert a new line into an existing text file(at the bottom).

    Is there an elegant way to do it(like Java classes that can do it)? I know there is the way of creating a new text file, writing the old text file over line by line, writing your new line, deleting old file, renaming new file to old file name.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    FileWriter has a constructor that may help you, but even if you use this, I think that it's evident that Java is doing just this in the background: completely re-writing the file. You can't get around the physics of hard disk storage.

  3. #3
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Maybe, but that way I don't need to rewrite something that's already there.

    Question solved.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Onra View Post
    Maybe, but that way I don't need to rewrite something that's already there.
    No, you don't understand. You are rewriting the file. You may think you're not but behind the scenes, this is exactly what Java is doing.

  5. #5
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Nah, I meant I don't need to reimplement the algorithm.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by curmudgeon View Post
    FileWriter has a constructor that may help you, but even if you use this, I think that it's evident that Java is doing just this in the background: completely re-writing the file. You can't get around the physics of hard disk storage.
    That is not true; nowadays file systems are very well capable of appending data to existing files without rewriting any previous content. File systems even exist(ed) that could prepend data to existing files but that capability never made it in main stream file system definitions. Only, if the last sector of a file wasn't completely filled it will be rewriten after new content has been appended to it.

    kind regards,

    Jos

Similar Threads

  1. Scanning text file and inserting contents into array
    By jmwalloh in forum New To Java
    Replies: 8
    Last Post: 03-24-2010, 01:33 PM
  2. Help! - How to insert a new line to a text file
    By matpj in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 06:28 PM
  3. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 02:01 PM
  4. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 08:21 PM
  5. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 01:31 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
  •