1

I'm writing a logger to my java program (in csv format) with bufferedWriter and FileWriter.

When i open the csv file while the program is running and continues writing to the file, I got this exception: "The process cannot access the file because it is being used by another process".

What i want is when i open the csv file while the program is running, The csv file will open in read mode and the program will writing successfully to the file.

I solved it by changing the closing of bufferedWriter and FileWriter to .flush() instead of .close()

Original minimal logger code (with the original close function)

public class logger {
     private BufferedWriter bw = null;
     private FileWriter fw = null;
     private File file = null;

   logger(String nclass) {

        path = "c:\\test\\test.csv";
        this.file = new File(path);

        // Check if the file is already exist.
        if (!file.exists()) {
            file.createNewFile();
        }

        fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);
   }
   public void writeToFile(String msg) {
    entryWrite();

    try {

        fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);
        fw.append(msg);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close();
        exitWrite();
    }
}

}

  private void close()  {
try {
    if (bw != null) {
        bw.close(); 
        bw = null;
    }
    if (fw != null) {
        fw.close();
        fw = null;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

}

My solution function

  private void close()  {
try {
    if (bw != null) {
        bw.flush(); 
        bw = null;
    }
    if (fw != null) {
        fw.flush();
        fw = null;
    }
} catch (IOException ex) {
    ex.printStackTrace();
}

}

Now my answer is if it is ok not to close the stream and just use with flush? Can there be any problems later? Because in all my tests its run well.

Thanks !!

Idon89
  • 141
  • 4
  • 16
  • 1
    Please show a full [mcve]; you are missing a lot of the important aspects. And for starters; I guess you are creating your BufferedWriter using the FileWriter; so why on earth do you call methods on both of those (you only need to flush/close the outer writer!) – GhostCat Mar 21 '17 at 08:38
  • @GhostCat - I Added. – Idon89 Mar 21 '17 at 08:56
  • Should be closed as DUP to http://stackoverflow.com/questions/16584777/is-it-necessary-to-close-a-filewriter-provided-it-is-written-through-a-buffered ... and the answer is spot on: this code is really ... sorry, bad. – GhostCat Mar 21 '17 at 08:58
  • @GhostCat - Why bad? I just creating a file and writing to him. What could I do better? I just want to take care with the problem that someone open the file while the program is still running... – Idon89 Mar 21 '17 at 09:12
  • @GhostCat - Ok, Thanks. – Idon89 Mar 21 '17 at 09:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138611/discussion-between-ghostcat-and-idon89). – GhostCat Mar 21 '17 at 09:31
  • 1
    I put some content into that chat room; have fun digesting ... and let me know what you think about it. – GhostCat Mar 21 '17 at 10:32

2 Answers2

2

Would it be ok some honesty?, in a very humble way. Sorry but stinky code:

  1. why setting to null "bw" and "fw"?, globals?, why?
  2. "ex.printStackTrace();" ? really?, no log4j or alike?
  3. why not using a finally block?, what happens if an exception occurs while reading writing the file?
  4. Someone has already answered this, for code refer to this excellent answer:

Is it necessary to close a FileWriter, provided it is written through a BufferedWriter?

Community
  • 1
  • 1
  • I added most of the code to my main post. 1. I dont know, Its a problem? 2. I dont need log4j because i write all the errors of the program to my logger. When i had a problam with the logger i see it live in java. 3. I used finally block when i finished to write to the file. look on my updated code. 4. Thanks. So you think it's safe to use flush instead close? – Idon89 Mar 21 '17 at 09:05
0

Just do bufferedWriter object close.

bw.close();

say, you have a large file.txt. Now u are gonna take something from a file and make a new smaller text file each time. in that case, somehow you have to write some lines of code to determine when you finish writing on a particular file. Then use flag to write bw.close();

eventually, you have to initialize the second file and do your task. then bw.close();

if you don't write fw.close(), then the file will be empty. so, make sure each file writing operation you have to write bw.close()

Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
Zobs
  • 31
  • 5