• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Reading File Problem

 
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
'


The TimeRecord.txt file content is:


1012012*
1022012*
1032012*
1042012*
1052012*
1062012*
1072012*


I want the time to be written in after those "*".
Thaks for the time and I'm hoping for your response guys.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anne Buena,

First things first : Welcome to CodeRanch!

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).

I hope this helps.
 
Anne Buena
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
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
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 hope this helps.
 
Anne Buena
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
For Example:

This is my file:
1012012*
1022012*
1032012*
1042012*
1052012*
1062012*
1072012*

Is it possible to do this:
1012012*
1022012*
1032012*text here
1042012*
1052012*
1062012*
1072012*

If yes, what are the methods I should use? I am using RandomAccessFile to manipulate my file.

I'm hoping for your patience. Thank you
 
Sheriff
Posts: 22208
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anne Buena
Greenhorn
Posts: 14
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh. Ok I'll try it ) Thank you very much!
 
Rob Spoor
Sheriff
Posts: 22208
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic