Note that there are some explanatory texts on larger screens.

plurals
  1. PORecord manipulation in flat files in Java
    primarykey
    data
    text
    <p>This is my first post here in StackOverflow. I am not a newbie to Java but I am not an expert or a professional programmer either. It has been quite some time I have had some ideas in my head and I do not know how to implement them in a proper way.</p> <p>In general I am writing a software (actually many individual applications) to manipulate a list of things (say a list of phone numbers). My application has to be contained within a single file(jar) or folder/directory. It has to be a "Remove out of box and Click and Run" application. Creating GUI in Java is not an issue.</p> <p>My problem is the storage of data. Do not suggest me to use any third party database server or application; since I am going to store the data in either a flat file in a normal format (or in an XML file).</p> <p>Currently 2 typical ideas come to my head for CRUDing (CRUD = Create Read Update Delete) the data in the file. They are as follows:-</p> <p>1 <b>Do not use a collection.</b></p> <ul> <li><b>create</b> - append record to file</li> <li><b>read</b> - read complete file</li> <li><b>update</b> - copy all records to new file except inplace of the one that needs to be replaced copy the new data; delete old file; rename new file to old file</li> <li><b>delete</b> - copy all records to new file except the one that needs to be deleted; delete old file; rename new file to old file.</li> <li><b>Advantage</b> : Less memory requirement. </li> <li><b>Disadvantage</b> : Lots of file IO.</li> </ul> <p>2 <b>Use a collection</b></p> <ul> <li><b>Application Start</b> - load all records into a collection from a file</li> <li><b>Application Stop</b> - save all records from collection into a file</li> <li><b>create</b> - add record to collection</li> <li><b>read</b> - read all elements of the collection</li> <li><b>update</b> - update record directly in the collection</li> <li><b>delete</b> - delete record from collection</li> <li><b>Advantage</b> : Very less file IO.</li> <li><b>Disadvantage</b> : Heavy memory requirements. Application DOES crash if there is no memory left while loading all records.</li> </ul> <p>Both methods have their pros and cons. Is there some other way? Or is there a way in between these 2 ways? I desperately need some guidance here. Been on this problem for a very long time. Any theories, suggestions or pointers are welcome!</p> <hr> <p>Would the following approach be fine? Would it be harmful or bad in any way? I mean what will be its disadvantage?</p> <p>Note: r --> Record. Each record is on a new line. The fields within each record are seperated by some delimiter say '::'. So I would use BufferedReader to get each line easily. The sizes are just hypothetical or just to give you a picture.</p> <p>File ={r1 r2 r3 r4 r5 ... r500} //the file has 500 records<br> Collection cPrev,cCurrent,cNext //3 collection objects holding consecutive records; each holding (say) 30 records<br></p> <p>So in the beginning<br> cPrev = {}<br> cCurrent = {r1 r2 r3 ... r30} //filled by main thread<br> cNext = {r31 r32 r33 ... r60} //filled by child thread while user viewing cCurrent<br></p> <p>cCurrent is viewable by the user. The user can scroll up and down (or whatever direction) and view all 30 records. Now user wants to see the next set of records. So<br> cPrev = cCurrent //main thread<br> cCurrent = cNext //main thread<br> Therefore<br> cPrev = {r1 r2 r3 ... r30}<br> cCurrent = {r31 r32 r33 ... r60}<br> cNext = {r61 r62 r63 ... r90} //filled by child thread while user viewing cCurrent<br></p> <p>Consider the follwing state<br> cPrev = {r121 r121 r123 ... r150}<br> cCurrent = {r151 r152 r153 ... r180}<br> cNext = {r181 r182 r183 ... r210}<br></p> <p>If user wants to see records before r151 then<br> cNext = cCurrent //main thread<br> cCurrent = cPrev //main thread<br></p> <p>So cPrev = {r90 r91 r92 ... r120} //filled by child thread while user viewing cCurrent<br> cCurrent = {r121 r121 r123 ... r150}<br> cNext = {r151 r152 r153 ... r180} <br></p> <p>Obviously, the next and previous can be performed as long as there are records after and before in the file. Performing the "next" operation is easy and simple. I would not need to close the file connection, and just start reading from where I left off. </p> <p>But what about the "previous" operation? the only solution that comes to my mind is [1] close current file connection [2] open new file connection [3] start reading from start of file till concerned record is reached and [4] then assign the set of records to the collection. (I don't know how to ask this) What is wrong with this approach? Plus, is there a better way or algorithm here? Guys, keep it simple but not compressed. I am no Java guru.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload