Modify the content of a file using Java
Asked Answered
P

6

14

I want to delete some content of file using java program as below. Is this the write method to replace in the same file or it should be copied to the another file.

But its deleting the all content of the file.

class FileReplace
{
    ArrayList<String> lines = new ArrayList<String>();
    String line = null;
    public void  doIt()
    {
        try
        {
            File f1 = new File("d:/new folder/t1.htm");
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);
            while (line = br.readLine() != null)
            {
                if (line.contains("java"))
                    line = line.replace("java", " ");
                lines.add(line);
            }
            FileWriter fw = new FileWriter(f1);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(lines.toString());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public statc void main(String args[])
    {
        FileReplace fr = new FileReplace();
        fr.doIt();
    }
}
Pismire answered 6/12, 2012 at 10:34 Comment(2)
i think you should use the name br only once ;)Simard
1) close the the reader after use: br.close(); 2) what is out in your code? 3) what is the result, what did you expect?Monandry
M
21

I would start with closing reader, and flushing writer:

public class FileReplace {
    List<String> lines = new ArrayList<String>();
    String line = null;

    public void  doIt() {
        try {
            File f1 = new File("d:/new folder/t1.htm");
            FileReader fr = new FileReader(f1);
            BufferedReader br = new BufferedReader(fr);
            while ((line = br.readLine()) != null) {
                if (line.contains("java"))
                    line = line.replace("java", " ");
                lines.add(line);
            }
            fr.close();
            br.close();

            FileWriter fw = new FileWriter(f1);
            BufferedWriter out = new BufferedWriter(fw);
            for(String s : lines)
                 out.write(s);
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String args[]) {
        FileReplace fr = new FileReplace();
        fr.doIt();
    }
}
Merrymerryandrew answered 6/12, 2012 at 10:45 Comment(1)
-1 I can't find any writeline in BufferWriter also Close should lower case.Kalif
D
14

The accepted answer is great. However, there is an easier way to replace content in a file using Apache's commons-io library (commons-io-2.4.jar - you can use any latest versions)

private void update() throws IOException{
        File file = new File("myPath/myFile.txt");
        String fileContext = FileUtils.readFileToString(file);
        fileContext = fileContext.replaceAll("_PLACEHOLDER_", "VALUE-TO-BE-REPLACED");
        FileUtils.write(file, fileContext);
 }

Note: Thrown IOException needs to be caught and handled by the application accordingly.

Doorplate answered 7/8, 2015 at 7:45 Comment(1)
Why is this better? It seems like it is doing the same thing... or at the very least just copying the file to a string and rewriting the file? Why do you need Apache for this?Borchardt
S
3

Read + write to the same file simulatenously is not ok.

EDIT: to rephrase and be more correct and specific - reading and writing to the same file, in the same thread, without properly closing the reader (and flusing the writer) is not ok.

Simard answered 6/12, 2012 at 10:41 Comment(2)
Could you please elaborate why ?Unwrap
Yes, generally speaking, a reason would be because the offsets within the file will shift everytime you make a write and the read cursor will not keep track of that (see the accepted answer here: #4251558). Still, after rereading this question, the problem here was not that (i answered too fast), but a mattering of not closing and flusing.Simard
D
2

Make sure to:

  • close any stream when you no longer need them
  • In particular before reopening it for writing.
  • truncate the file, to make sure it shrinks if you write less than it had.
  • then write the output
  • write individual lines, don't rely on toString.
  • flush and close when you are finished writing!

If you use buffered IO, you always have to ensure that the buffer is flushed at the end, or you might lose data!

Diplopia answered 6/12, 2012 at 10:39 Comment(1)
I'm not sure he is reading/writing at the same time. He reads the lines into a list, then writes the list to file, or at least is attempting to.Horal
H
0

I can see three problems.

First you are writing to out which I assume is System.out, not an output stream to the file.

Second, if you do write to an output stream to the file, you need to close it.

Third, the toString() method on an ArrayList isn't going to write the file like you are expecting. Loop over the list and write each String one at a time. Ask yourself whether you need to write newline characters as well.

Horal answered 6/12, 2012 at 10:45 Comment(0)
A
0

The accepted answer is slightly wrong. Here's the correct code.

    public class FileReplace {
List<String> lines = new ArrayList<String>();
String line = null;

public void  doIt() {
    try {
        File f1 = new File("d:/new folder/t1.htm");
        FileReader fr = new FileReader(f1);
        BufferedReader br = new BufferedReader(fr);
        while ((line = br.readLine()) != null) {
            if (line.contains("java"))
                line = line.replace("java", " ");
            lines.add(line);
        }
        fr.close();
        br.close();

        FileWriter fw = new FileWriter(f1);
        BufferedWriter out = new BufferedWriter(fw);
        for(String s : lines)
             out.write(s);
        out.flush();
                } 
        out.close();
   catch (Exception ex) {
        ex.printStackTrace();
    }
}
Airdrop answered 14/10, 2016 at 10:51 Comment(2)
Yes. "out.close()" should be out of "for" loop.Airdrop
It is outside of the for loop. The for loop is only the next line, as the braces have been emitted. You have put it outside of the try block. A better solution would have been to put the readers inside using{...} blocksFriday

© 2022 - 2024 — McMap. All rights reserved.