Hey folks,
I have come upon what seams to be a very basic problem, but I have read my text book over and over and I can't figure out how to write to a specific line of a text file.

For example each line of my text file looks like: 1,29 What I want to do is set the 1 to zero, but I don't know the syntax for editing specific lines, just adding lines to the bottom of the file.

Any help would be great!
PO.

Recommended Answers

All 3 Replies

Honestly even I have not been able to encounter any such method of editing files. What I do is read the contents of the original file into a String or StringBuffer, using the methods of the String or StringBuffer class perform the required string manipulation on the file content, write this content to a temporary file, then delete the original file and rename the temporary file with the name of the original.

Although this work around would work, I would still suggest waiting to see what the likes of Ezzaral or Masijade would suggest, because I haven't really messed around with files as much in Java.
<EDIT>
Eeeps !!! forgot to mention ~s.o.s~ in that list, any way I guess you now know from the below post why I suggested you wait for either of their suggestions.

> I can't figure out how to write to a specific line of a text file.

An efficient way would be to open the file using a RandomAccessFile in read/write mode though things can get pretty nasty if care is not exercised. Almost all other methods include reading a line from the target file, processing it and then writing it to a destination file.

Okay well, I will describe my problem. I have a bunch of accounts for a program, but i have more computers than programs.
Since I can add a login parameter to the executable I wrote a program that has a list of the accounts in that are comma delimited all starting with 0.
What I want is to have the program change the 0 to a 1 if a computer has taken that account.

So in basic really what I need is a way of determining if an account is being used or not.

PS.
I am using this program in an internet cafe where I work as a technician if anyone was curious :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.