Hi all,
I have a question, how can i change one line in txt file with file writer in java?
I got txt file with for example 10 lines of strings, how can i edit line 5 to write something else.I was looking on Internet for solution, there was something about copy file into another and then return data.Is there other simpler solution?
Thanks in advance.

You can not arbitrarily pick a line in a file and change it. Capi will probably have a better explanation that I will, but here goes. A file is data written to a disk. There is no guarantee that it will be written to contiguous memory. When you look at a text file, you are looking at the result after all of the pieces have been pulled from memory. Therefore the system has no idea what "line 5" means. At any rate, in order to change something in a file, you need to read the file into memory.

In the case of a text file, all you need to do is read the file line by line and dump it into a String Array. Change the line of text that you want changed, then write everything back to the file, overwriting the previous data. It is pretty simple really.

The problem here is essentially that, in the general case, you can't directly index a file by line number. A file, whether it be text or binary, is a collection of bytes. You can seek to a specific byte inside the file, you can read 10 bytes or overwrite 25 bytes, but you can't say "I want to replace line 7".

The reason for this isn't so much that the file might not be physically written contiguously on the disk (the operating system takes care of that for you). Rather, the reason is that files are indexed by (made out of) bytes, not text lines. Why? Amongst other things, because text lines are not constant in size.

In order to find out where a line ends and the other begins, someone needs to read the text characters from the beginning, looking for the newline sequence. The operating system doesn't do that for you, so you need to do it yourself. Then there's the problem of altering the line: unless you replace it with something with the same exact length in bytes, you will either overwrite part of the next line, or leave old junk behind.

As for doing what you want, you may take several approaches. One way to do it, as Groovicus noted, is to read the whole file first into an array of lines, process the array then write the whole thing to a new file (or replace the old one). Another way would be to take a streamed approach: read one line, process it if necessary, write it to the new file, read another line, process it if necessary, write it to the new file, etc. This is slightly more complex, but it has the added benefit of not having to buffer the entire file in memory, which means you can handle arbitrarily large files (as long as the lines of text aren't arbitrarily large ).

Of course, depending on exactly what you want to do, you may be able to take some shortcuts for specific cases. Say you only need to replace a specific sequence with another sequence of the same length. You could treat the file as a stream of characters; read the file from the beginning, counting newlines until you reach the desired line, then look for the offset in that line. The total number of characters you read gives you the offset you need to seek into the file in order to write the replacement sequence.

If you know that all the lines in the file are of a specific length, it's even easier; you don't even need to read the file. You could do the math (number_of_lines * line_length + offset_in_the_desired_line) to find the bytes you need to overwrite. Then just seek to the desired position and write your sequence, overwriting what's already there.

Sorry capi. I know you spent a bit of time on that post. I am not sure that one can accomplish your method with Java though. I have not thought too much about it though, and since it is so easy to do it at a higher level of abstraction, I doubt I will think about it at all.

I am not sure that one can accomplish your method with Java though. I have not thought too much about it though, and since it is so easy to do it at a higher level of abstraction, I doubt I will think about it at all.

Oh, which of the methods were you referring to? I suppose you mean the last couple of ones, where you seek into the file, yes? Well, those were meant more as examples of different approaches to specific cases, to open up the realm of possibilities a little bit. My first recommendations were the two more traditional methods (including your own), which would be easier to implement and the only ones that work for the general case.

The file seeking approaches do have a definite advantage in the specific cases they are meant to handle, though. Although they force you to think a bit more in terms of byte streams and positioning, they have the important advantage of being able to process arbitrarily large files in constant time -- that is of course for the last method; the one before it needs to scan until it finds the line to be modified, but you don't need to touch any further lines after that.

In any case I am admittedly not a Java expert (and don't want to be!), but I assume it should let you seek into a specific position of a file -- you can easily do it with Python, for example, which is also very high level (of course that's an unfair comparison, since Python is actually good )

Indeed memory-mapped IO is an excellent and easy way to edit a file in-place. The only problem, as with any other kind of inline text editing, is that if you want to edit a single line, you need to make sure the new contents are the same length. Otherwise you'll leave junk at the end of the line, or overwrite the next one.

Or of course, you can just move all the data after the line "to the right" or "to the left", as necessary.