Edit a file using RandomAccessFile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Edit a file using RandomAccessFile

    Hi All ....
    I am trying to edit a file using RandomAccessFil e.
    I am having a little bit problem.

    My file is ...
    Code:
    i m debasis jana.
    [code=java]
    RandomAccessFil e file = new RandomAccessFil e("file_name"," rw");
    file.seek(10);
    file.write("aaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa a".getBytes( ));
    [/code]

    My edited file is ....
    Code:
    i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    Where as I expected as .......
    Code:
    i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaais jana.
    Please help me.
    I can do this by saving the last file content.
    But I want this by using a simple API.

    Debasis Jana.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    To my knowledge, randomAccess file does not mean "inserting" characters, but it just overwrites, starting at the given position.

    In your case, if it reaches beyond the file end, it also appends.

    if you would insert "xxx" only, you should see:
    i m debasxxxjana.

    usually, you insert "records" there which all have a fixed size. A record is a chunk of data with a fixed size,f or example a byte array.
    So if you want to make a change in a record, you completely overwrite the old one with the new one.

    A file can consist of thousands of records. So to make changes, you don't need to read he whole file data (all records), modify the desired one and write back the whole file data (all records), which is done in a sequential way, but you can seek to the desired record position and only overwrite the data there, which is much, much faster. That's why it's called "random access".

    so "inserting" is not possible easily; if you want to insert, you must shift all records that are behind the inserted data, which takes a long time.
    Because of that, you usually only append new records to the end, or remember the gap of a deleted record and insert the new record there.


    Originally posted by dmjpro
    Hi All ....
    I am trying to edit a file using RandomAccessFil e.
    I am having a little bit problem.

    My file is ...
    Code:
    i m debasis jana.
    [code=java]
    RandomAccessFil e file = new RandomAccessFil e("file_name"," rw");
    file.seek(10);
    file.write("aaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa a".getBytes( ));
    [/code]

    My edited file is ....
    Code:
    i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    Where as I expected as .......
    Code:
    i m debasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaais jana.
    Please help me.
    I can do this by saving the last file content.
    But I want this by using a simple API.

    Debasis Jana.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by BigDaddyLH
        My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.
        Thanks for your suggestion.
        What do you mean by RAF passes?
        One more thing I did solve that problem by using the StringBuffer.
        I just wanted to know was there any way to do with RandomAccessFil e only?

        Debasis Jana

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by dmjpro
          Thanks for your suggestion.
          What do you mean by RAF passes?
          One more thing I did solve that problem by using the StringBuffer.
          I just wanted to know was there any way to do with RandomAccessFil e only?

          Debasis Jana
          "lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFil e is usually a bad idea. What a little while and you will get a better idea of what to do instead.

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by BigDaddyLH
            "lie down until the urge to use RAF passes" -- a jokey way of saying a RandomAccessFil e is usually a bad idea. What a little while and you will get a better idea of what to do instead.

            Hahahaa ............. So funny!

            Debasis Jana

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

              #7
              Originally posted by BigDaddyLH
              My rule of thumb with random access files is that there the wrong answer, no matter what the question was! On one extreme, the programmer is in database denial, thinking they can get away with a flat file solution, and on the other extreme, they haven't thought about the design enough. Either way, my advice is to lie down until the urge to use RAF passes.
              Maybe you should elaborate your "experience ", or else people don't understand how you achieved this opinion.

              If you have a stand-alone program, and a huge amount of data (hunded of megabytes), it's faster to just store the data in a RAF and care for it yourself than accessing a database and let the database store it for you. Especially if you don't want to install a database or configure it.
              This means faster way of storage and less code to write.
              Also if you write a database-application, you need RAF, which is used internally in every database.

              On the other side, a server can write several megabytes of data in one second, so if your data size is less, it's better to read the whole data in, manipulate it and then write it back.
              Then you don't need to care about fixed sizes of your records (you can easily increase/decrease size), no need to care about too many gaps inside the data and reducing them by shifting records around etc. Also if you need to update several hundred records at once, then chasing the hard drive head around is much,much slower than writing the whole file at once. On the other side, if you need to update only one record in a while, then RAF is faster.

              Usually it's better to save a huge amount of data in multiple small files than having all data in one file, so a "design change" is better than using RAF.
              Just think of data corruption: Losing one small file, containing one record, is better than losing the big file that contains all records.
              For example email saved in Thunderbird (all small files) or in Outlook (one big file).

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by chaarmann
                Maybe you should elaborate your "experience ", or else people don't understand how you achieved this opinion.

                If you have a stand-alone program, and a huge amount of data (hunded of megabytes), it's faster to just store the data in a RAF and care for it yourself than accessing a database and let the database store it for you. Especially if you don't want to install a database or configure it.
                This means faster way of storage and less code to write.
                Also if you write a database-application, you need RAF, which is used internally in every database.

                On the other side, a server can write several megabytes of data in one second, so if your data size is less, it's better to read the whole data in, manipulate it and then write it back.
                Then you don't need to care about fixed sizes of your records (you can easily increase/decrease size), no need to care about too many gaps inside the data and reducing them by shifting records around etc. Also if you need to update several hundred records at once, then chasing the hard drive head around is much,much slower than writing the whole file at once. On the other side, if you need to update only one record in a while, then RAF is faster.

                Usually it's better to save a huge amount of data in multiple small files than having all data in one file, so a "design change" is better than using RAF.
                Just think of data corruption: Losing one small file, containing one record, is better than losing the big file that contains all records.
                For example email saved in Thunderbird (all small files) or in Outlook (one big file).
                As you like... I'll stick with databases.

                Comment

                Working...