Jump to content

Idea for changing one line in a flat file (JAVA)

gastew15
Go to solution Solved by gastew15,

Just ideas on how to amend one line in a file that is already written..

It would be used for someone to input a string & say a line number and it would replace that line number with that new string.

 

EX:

(File)

 

Bob

Jane

Billy -

Tom

George

 

I want to turn it into this

(File)

Bob

Jane

Freddrick -

Tom

George

 

Edit: I guess I made this thread a little too early as I just thought of the fact that I can read the file at load up into an array & then just amend to that array and then just print the array to the file any time it changes.. Sorry for wasting anyone's time..

I've been trying out different ways in which to do the above, mostly resulting in sadness, without too much luck. Currently I think the best solution may be to copy everything from the one and place it in a temp file in which I change the line when I write it back to the original and then delete the temp file. This seems a little bit ghetto to me and I'm curious about some better solutions. I'd like to thank anyone in advance as I don't usually work with file writing / reading besides log files and much very often.

 

Edit: I may just be able to read the file into an array at start up & then update it with the file.

"Her tsundere ratio is 8:2. So don't think you could see her dere side so easily."


Planing to make you debut here on the forums? Read Me First!


unofficial LTT Anime Club Heaven Society

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know what you're talking about, maybe get more detailed into what you're doing and what you need help with maybe?

Link to comment
Share on other sites

Link to post
Share on other sites

Just ideas on how to amend one line in a file that is already written..

It would be used for someone to input a string & say a line number and it would replace that line number with that new string.

 

EX:

(File)

 

Bob

Jane

Billy -

Tom

George

 

I want to turn it into this

(File)

Bob

Jane

Freddrick -

Tom

George

 

Edit: I guess I made this thread a little too early as I just thought of the fact that I can read the file at load up into an array & then just amend to that array and then just print the array to the file any time it changes.. Sorry for wasting anyone's time..

"Her tsundere ratio is 8:2. So don't think you could see her dere side so easily."


Planing to make you debut here on the forums? Read Me First!


unofficial LTT Anime Club Heaven Society

Link to comment
Share on other sites

Link to post
Share on other sites

use a bufferedreader to read all the file content up until the chosen line when it hits the line store it into some sort of format and then add the line he wants to add after that skip the line of the original and copy the rest and add it onto the stored content and replace the original with that

Link to comment
Share on other sites

Link to post
Share on other sites

Another way to go about doing this is using a LineNumberReader and a Writer for the output. You can implement a simple algorithm that reads through the lines looks at the line number and if its equal to the one to be replaced then you output your line instead of the read one.  This will scale much better for working on files that are large, potentially Gigabye's large and it will spread the IO over time as well so it should end up faster as well. Its more complicated than just reading to an array, replacing and writing out but it has a lot of benefits as an approach

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×