How to update data inside a text file in Java ?

T

tobleron

Hi,

I have a text file named user.txt that contains data like this :
========
test;myecg;
test1;pass1;
tes2;mypass;
========
The first column is userID and the second column is password. I want
to write a Java code to update the password for user number 1, for
example become like this :
========
test;newecg;
test1;pass1;
test2;mypass;
========
I know that java.io has classes to read and write a text file. But how
to update the data inside a text file ? Is there anyone can help ?
Thanks in advance.

best regards,
Budhi
 
R

RedGrittyBrick

tobleron said:
Hi,

I have a text file named user.txt that contains data like this :
========
test;myecg;
test1;pass1;
tes2;mypass;
========
The first column is userID and the second column is password. I want
to write a Java code to update the password for user number 1, for
example become like this :
========
test;newecg;
test1;pass1;
test2;mypass;
========
I know that java.io has classes to read and write a text file. But how
to update the data inside a text file ? Is there anyone can help ?
Thanks in advance.

I'd do it the way you'd do it in any programming language.

create a temp file (unique name e.g. PID based)
repeat
read record from current file
perform string substitution if needed
write record to temp file
until EOF
rename current as backup
rename temp as current
optionally delete backup

It's your OS and it's filesystem that disallow anything simpler.
Exceptions will exist where the replacement is guaranteed to be the same
length, then you may be able to seek and write using something like
RandomAccessFile.
 
J

John B. Matthews

tobleron said:
Hi,

I have a text file named user.txt that contains data like this :
========
test;myecg;
test1;pass1;
tes2;mypass;
========
The first column is userID and the second column is password. I want
to write a Java code to update the password for user number 1, for
example become like this :
========
test;newecg;
test1;pass1;
test2;mypass;
========
I know that java.io has classes to read and write a text file. But how
to update the data inside a text file ? Is there anyone can help ?
Thanks in advance.

Aren't you using a database? Is this patient data? No time like now to
encrypt those passwords:

<http://www.owasp.org/index.php/Hashing_Java>
 
A

Arne Vajhøj

tobleron said:
I have a text file named user.txt that contains data like this :
========
test;myecg;
test1;pass1;
tes2;mypass;
========
The first column is userID and the second column is password. I want
to write a Java code to update the password for user number 1, for
example become like this :
========
test;newecg;
test1;pass1;
test2;mypass;
========
I know that java.io has classes to read and write a text file. But how
to update the data inside a text file ? Is there anyone can help ?

You should probably use something else than a flat file for this. Like
a database.

In general (all widely used file systems and no assumptions about
length of password) you will have to rewrite the file (technically
only from the point of change and forward, but you can just as well
rewrite the entire file then).

If passwords are always the same length then you could update
by using the RandomAccessFile class and some assumptions about
the record structure (\n or \r\n terminated etc.).

Arne
 
T

tobleron

Actually I used database in the server side. But for this case, this
is for client side. I'm not using database, because i just need to
authorized the user when the network connection is down. So the user
still can be authorized by local data. I'm avoid to using database in
order to keep my application portable and light, because this
application is designed to be implemented in all OS, so installing a
database in client will bias the target.

Here is the scenario :
- User log in to the application. When network runs well, it will be
authorized by database in the server, and user can work online and
real time.
- But when network fails, user will be authorized by small local data
in the client side (which is stored in text file), so user still can
work off line. The working data also will be stored locally and
temporary in the client side. Why user still need to be authorized ?
Because this is medical application, authorization of the user is
mandatory, even he/she works off line.
- When the network is ON, then all temporary data will be transferred
into the server, and the temporary data will be erased.

So, When user change the password, the application should be able to
changes the password that stored in the database (server side) and in
the text file (client side) in the same time. There is no problem in
the server side, but I don't know how to update the text file (client
side). In know how to do it in VB, but this is Java, i am not quite
understand how to do it in Java. Please help.
 
R

RedGrittyBrick

tobleron said:
Actually I used database in the server side. But for this case, this
is for client side. I'm not using database, because i just need to
authorized the user when the network connection is down. So the user
still can be authorized by local data. I'm avoid to using database in
order to keep my application portable and light, because this
application is designed to be implemented in all OS, so installing a
database in client will bias the target.

Here is the scenario :
- User log in to the application. When network runs well, it will be
authorized by database in the server, and user can work online and
real time.
- But when network fails, user will be authorized by small local data
in the client side (which is stored in text file), so user still can
work off line. The working data also will be stored locally and
temporary in the client side. Why user still need to be authorized ?
Because this is medical application, authorization of the user is
mandatory, even he/she works off line.
- When the network is ON, then all temporary data will be transferred
into the server, and the temporary data will be erased.

So, When user change the password, the application should be able to
changes the password that stored in the database (server side) and in
the text file (client side) in the same time. There is no problem in
the server side, but I don't know how to update the text file (client
side). In know how to do it in VB, but this is Java, i am not quite
understand how to do it in Java. Please help.

In my view, that is more a pretence of security than real security. As
such it is worse than no security, since it will mislead users of your
medical application into believing that the application is protected
when it isn't.

I hope you are storing hashes (e.g. SHA1 or MD5 hashes) of passwords and
not plaintext passwords!

I hope I never fall into the hands of a medical facility that uses
medical applications which store plaintext passwords in text files on
desktop PCs.
 
T

tobleron

In my view, that is more a pretence of security than real security. As
such it is worse than no security, since it will mislead users of your
medical application into believing that the application is protected
when it isn't.

I hope you are storing hashes (e.g. SHA1 or MD5 hashes) of passwords and
not plaintext passwords!

I hope I never fall into the hands of a medical facility that uses
medical applications which store plaintext passwords in text files on
desktop PCs.

Of course I'll use encryption like MD5. The application is still in
development. Don't worry :)

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top