Hi! Good Day. I am doing a Payroll System. I have this class names EmpLogin.java. What it does is it allows the Employees enter their Employee code. When the OK button is clicked, I want the program to read a file named TimeRecord.txt where all the employee code is written. Example: Employee entered 1012012. I want the program to search for that specific employee code then add the current time at the end of that line.
I'm thinking to put the contents of file in an array. But I'm really confused how i can do this and how can i add the time in each line.
So here's the code. Sorry for all those naughty comment lines.
It's good to see that you are using CodeTags in your posts
I assume that you'll insert your code at line 86.
Now, I have few questions:
1) How big is the file? I mean, what is the possibility that the content of complete file can be loaded in memory (i.e. JVM's memory)?
2) Is there any possibility that multiple threads can/will edit this file?
3) What is the frequency of editing the file? And where else would this data be used?
I'm asking this because, to guarantee data integrity, the best approach would be to create the file content in memory and dump it at a time to a new file, deleting old file.
So, if the read/writes are too frequent, another apporach like in-memory-file can be used (i.e. keep all the data in memory, keep on doing operations on it and dump the data after some regular interval - time interval or after some specific number of operations).
Yes. I'm supposed to enter the code in line 86. But I don't have any idea what to do next.
I can't say how big the file will be because I have another class that adds employee. It really depends on how many employee will be added. And the data that will be written in TimeRecord.txt are the time only. It's only for project use so I think during the program testing of my prof, Time In and Time Out will be done once or twice only.
The file TimeRecord.txt will be access everytime someone enter her/his Employee Code to Time in/Time out.
Do you have any idea or advice there? I am having a hard time thinking what. Thank you
Ok. I can suggest two approaches in that case:
1) Maintaining temporary file in memory and dumping it occassionally (the one I've already mentioned). However, you'll have to take care about abrupt failures (say you are holding last 5 transactions in memory and system fails)
2) Second approach is time consuming, it will hamper performance, but will guarantee data integrity : while inserting record for the first time, create a new file, and put time as 00 etc. for all records and delelte old file. This would be one time activity. After this, each time, you'll only have to edit those characters with actual time. Here, you won't be actually inserting anything, so it should work flawlessly.
I am eyeing the second approach. Pardon me, but can you tell me what are the algorithms or steps that I should do for your second option?
Another thing, is it possible to write in the end of each line?
This is my file:
Is it possible to do this:
If yes, what are the methods I should use? I am using RandomAccessFile to manipulate my file.
You shouldn't edit the existing file, but instead do the following:
- Create a new temporary file. You can use File.createTempFile for that.
- Write to that temporary file.
- Delete the original file.
- Rename the temporary file to the original file.