Results 1 to 2 of 2
  1. #1
    stringargs is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default write to file one line at a time and keep all contents

    So, say I have a text file like this:

    aaaaaaaaaaa
    bbbbbbbbbbb
    ccccccccccc

    I need to replace the first line with "cccc" and I need that done without copying the whole file, its contents or making a new file and renaming it later.
    Ideas?

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    It's easy enough to avoid renaming it (just open it, read it, close it, change it, open it again, write it, and close it again), but what's wrong with copying its entire contents? It's easier to do that with a BufferedReader & BufferedWriter than using something like a RandomAccessFile, which is a pain in the ass, and is better suited to binary files than to plain text ones.

Similar Threads

  1. Write to a specific line in a file
    By phantom06 in forum New To Java
    Replies: 1
    Last Post: 02-05-2011, 06:32 PM
  2. write line multiple times
    By relith in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 08:38 AM
  3. Read and Write depending on file contents
    By paulhenderson in forum Java Applets
    Replies: 3
    Last Post: 02-15-2010, 12:38 PM
  4. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 02:44 AM
  5. Write to line in file
    By blackstormattack in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 12:59 PM

Posting Permissions

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