dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Append file at the beginning

  1. #1
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Append file at the beginning

    Hi all,

    I'd like to know if there are any standard way in Java to append text to a file at the beginning of it.
    The only occurs to me is use a temporal file to store the original file, overwrite it with new text, and the append the contents from temporal. Any other idea?

    Thanks

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Append file at the beginning

    Generally the best approach is pretty much as you've described. There is a another way that maybe suitable in very particular cases.

    If you know how much data you are likely to need to prepend you could write the original file starting with blank spaces that you can later overwrite using RandomAccessFile.

  3. #3
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Append file at the beginning

    Quote Originally Posted by keang View Post
    If you know how much data you are likely to need to prepend you could write the original file starting with blank spaces that you can later overwrite using RandomAccessFile.
    It's not my case. In fact, I need to create a service (method) which must be able to write into a file in any position of it. Like this:
    Code:
    public appendFile(String path, String contents, long position)
    where position can be 0L, which means at the beginning, or any other value lower than the length of file. So, I suppose there is no other way than create a temporal file.

    Thanks

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)