tags:

views:

52

answers:

2

Hello guys!

I need to search a file for a word and return the whole line and the line number with this word, then edit the line and write back to the file. Maybe the line number isn't necesary to edit a line in a file. I `was reading after seraching with regexp and opening the filechannel of the file, but I can't get the line number. Maybe there are other better ways to do this. Can you help me how to start this?

+1  A: 

First to avoid a potential misconception, you can't edit a file on the fly by just pointing out the line number and changing the line. You would need to read the whole content and write the whole content back to the same file at any way, even though you only need to edit a single character.

As to the line numbers, you can just add an incremental counter to the loop wherein you read each line of the file using BufferedReader#readLine(). You can also use the LineNumberReader, but this doesn't really work as starters would expect. So does for instance setLineNumber() actually not skip the lines to the given line.

BalusC
I did some research and I found on a site, that I can edit a file with RandomAccessFile. Maybe that isn't true. What do you think? I need to try it.
Infinity
Ok, so here is it: http://java.sun.com/docs/books/tutorial/essential/io/rafs.htmlYou can read this:"The following code snippet opens a file for both reading and writing by using one of the newByteChannel methods."
Infinity
The difference with `RandomAccessFile` is that you need to know the **byte** position rather than the line number. This isn't very useful when dealing with **character** data / lines.
BalusC
OK, that's true, but is there any good solution? Or can you recommend me something? Because I don't want to read the whole file and then rewrite it.
Infinity
To rewrite it you need to read the whole file at any way. Also see the first paragraph. You can't just do `output.write(newline)`. This would overwrite the entire file with just that line.
BalusC
I don't want to rewrite the whole file. Just a part from it and that's can be done with the RandomAccessFile, but I need to get the location in the file where I want to write my text.
Infinity
Once again, it cannot be done with `RandomAccessFile` as well. It's just to **read** certain byte ranges from a file. To **write** the file, you need to have an `OutputStream` or `Writer` as well. And yes, you need to feed it with the entire stream. Read the first paragraph of my answer once again. This is just a limitation how file systems work.
BalusC
Ok, if there is a limitation by the system, maybe I don't need to do it manually. I think here comes the RandomAccessFile which will do the work for me. Isn't it? And what do you think the about the fopen command in c which also can open the file for input and output?
Infinity
A: 

Why not sth. like:

   BufferedReader reader = new BufferedReader(new FileReader(file));
   BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

    int lineNo = 0;
    String line;        
    while ((line = reader.readLine()) != null) {
        lineNo++;
        if (line.contains("word")) {
            //manipulate line
            line = "changed content";
        }
        writer.write(line);
    }        

    reader.close();
    writer.close();

or do the writing after you read the whole file ...

Warning: for proper encoding handling you should use:

new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
// the same for the writer
rocker