Cannot copy a file using "File.copy" method in java

381 Views Asked by At

I am new to Java.

While I am trying to copy a file using "File.copy" method in java. The process end without errors.

But the file I need to copy is created with only one weired character (I think it is an file end character). and it has no content other than it.

Here is my source code,

var sourceFile = new File("mySourceFile.txt")
var destinyFile = new File("myDestinyFile.txt");
Files.copy(sourceFile.toPath(), destinyFile.toPath());
2

There are 2 best solutions below

0
Sakthimahendran On BEST ANSWER

I solved the problem.

The problem is that there are outputstreams already open for the same destiny file I have to.

So it File.copy() method cannot write on the file.

2
Kevin On

it doesnt matter if the file you will copy has one or hundred character. You say the process ends without errors. Does it mean it works fine or it does someting wrong but you get no error. If this is the case you can try to take the full path like this:

    var sourceFile = new File("C:\\Users\\Kevin\\test1.txt").toPath();
    var destinyFile = new File("C:\\Users\\Kevin\\test2.txt").toPath();
    
    Files.copy(sourceFile, destinyFile);