Moving / copying a file to another directory creates an empty file and empty sources

My goal is to move the file from one directory to another. The source is on the local drive, and the destination is on the network drive. It doesn't matter if I move or copy and then delete the source. The file is about 6 GB.

What I tried:

 // C:\path\to\dir\file.bak
File source = new File(localRoot + backup);
// \\192.168.1.100\path\to\dir\file.bak
File dest = new File(storageRoot + "/" + storagePath + "/" + backup); 
try {
    log("Copying");
    // I've tried copyFile as well.
    FileUtils.copyFileToDirectory(source, dest);
    log("copied");
} catch (Exception e) {
    e.printStackTrace();
}

File source = new File(localRoot + backup);
File dest = new File(storageRoot + "/" + storagePath + "/" + backup);
try {
    log("Copying");
    // I've tried move and creating Paths instead of Files as well.
    Files.copy(source.toPath(), dest.toPath());
    log("copied")
} catch (Exception e) {
    e.printStackTrace();
}

I also tried a manual method using Input, OutputStreams and reading bytes.

The results are that the file is created at the destination with the correct file name with 0 bytes, and the original file is overwritten from 6 GB to 0 bytes. This happens for all the methods I tried, the only exception is that when I tried to move, the original file was deleted and not overwritten.

, , . , ?

+4

Source: https://habr.com/ru/post/1620428/


All Articles