2

I have seen this question many times but I have not found a solution that works for me. I am trying to rename the file to a new file name. I am getting false every time and am not sure why like most others with the same problem.

my code is as follows:

File file = new File("filePath.log");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));

for(int i = 1 ; i < 300 ; i++){
    bw.write(i);

    if(i % 100 == 0){
        File newFile = new File("filePath2.log");
        if(file.renameTo(newFile)){
             System.out.println("true");
        } else{
             System.out.println("false");
        }
        file = new File("filePath.log");
        bw = new BufferedWriter(new FileWriter(file));
    }
}

Any help is appreciated!

EDIT:

I was able to fix the problem using Files.move method

bw.close();
String newFilePath  ="C:/opt/streamserve/projroot/applications/RFC_SAP_T1/wd/rfcgateway07122014" + move + ".log";
File newFile = new File(newFilePath);
Files.move(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
file = new File("C:/opt/streamserve/projroot/applications/RFC_SAP_T1/wd/rfcgateway.log");
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
move++;
ola
  • 882
  • 2
  • 12
  • 29

4 Answers4

4

On windows an open file is implicitly locked. If you don't want this feature, don't use windows (I assume there is a hack you can do to not have this happen)

The problem is you can't rename a file you have locked. Under Unix, the file is only locked if you did this explicitly, in windows it is locked just by opening it.

The solution is always close your file before attempting to rename it, it is good practice even on Unix.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

You will need to close the BufferedWriter. Otherwise, the file is in use and cannot be renamed (at least on a maschine running windows)

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
0

well, closing BufferedWriter, your file will be saved on disk and you can have access to it.

you can obtain this invoking close method for BufferedWriter:

File file = new File("filePath.log");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));

for(int i = 1 ; i < 300 ; i++){
    bw.write(i);

    if(i % 100 == 0){
        File newFile = new File("filePath2.log");
        bw.close();
        if(file.renameTo(newFile)){
             System.out.println("true");
        } else{
             System.out.println("false");
        }
        file = new File("filePath.log");
        bw = new BufferedWriter(new FileWriter(file));
    }
}
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
blurstream
  • 429
  • 3
  • 13
0

You need to close BufferedWriter otherwise it will not write to file. your file will be empty. Try the following:-

    File file = new File("filePath.log");
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    for(int i = 1 ; i < 300 ; i++){
        bw.write(i);
        if(i % 100 == 0){
            bw.close();
            File newFile = new File("filePath"+i+".log");
            if(file.renameTo(newFile)){
                 System.out.println("true");
            } else{
                 System.out.println("false");
            }
            file = new File("filePath.log");
            bw = new BufferedWriter(new FileWriter(file));
        }
    }
    bw.close();
Swaraj
  • 589
  • 4
  • 15