4

I'm coding a Java Application right now and it has to read through a file. I'm using this method to do so:

BufferedReader mb_reader = new BufferedReader(new FileReader(f1));
int lines = 0;
while (null != (mb_line = mb_reader.readLine())) {
    lines++;
    //something to do
}

Everything works fine but it's possible that the file has to be changed from outside (for example).

I have a mthod that tests if the file exists and then open this reader. But if I now try to rename the file in the Windows Explorer it just says that the file is opened in Java and can not be renamed.

Is there a way to make it possible to rename it even if it's opened?

JetStream
  • 809
  • 1
  • 8
  • 28
  • 2
    one logical way would be to make a copy for yourself before you acquire lock, so original file is free for edit externally. – user1933888 Jul 31 '15 at 10:35
  • In any case you will not be able to find your file again to possible write it back, when it has been renamed from outside. – Uwe Allner Jul 31 '15 at 11:10

3 Answers3

2

You need to close the stream to release the file to the system, i don't think there's a straightforward way around this.

You could have a working copy to read from and check the file every now and then for changes you might expect.

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
  • Ok and where to copy it? Is there a way to copy it inside the program (maybe inside the .jar)? – JetStream Jul 31 '15 at 11:05
  • Well, for details about that you can ask another question. As to where you can copy it, it can be in the RAM memory. There are of course lots of consideration for this, especially performance wise. That is why a new question needs to impose use constraints. – Laurentiu L. Jul 31 '15 at 11:33
  • What you shoould ask yourself tho is if you really want to go to such lenghts to unlock the file and decide if it really is necessary before proceeding. – Laurentiu L. Jul 31 '15 at 11:36
  • Thank you, I solved the problem by using `File temp = Files.createTempFile("temp", ".txt");` and then copying my file to this temporary one by calling `FileChannel src = new FileInputStream(file).getChannel();`, `FileChannel dest = new FileOutputStream(temp).getChannel();` and `dest.transferFrom(src, 0, src.size());`. After that I just had to close the two created Streams and was able to work with the `temp` file and the file `file` could be edited outside the program. Thank you :) – JetStream Jul 31 '15 at 12:36
0

Maybe you should read the file first totally into a StringBuilder or similar if its not too large then release it so the other thread or process can access it. I mean reading the file can be pretty fast compared to holding it open while you process it completely.

breakline
  • 5,776
  • 8
  • 45
  • 84
0

If only the file name has to be changed while reading but the content stays the same, I would create a temporary copy of that file and read from that copy.

This way you wouldn't lock the original file and still can access the content.

das Keks
  • 3,723
  • 5
  • 35
  • 57