The example below show you how to copy a file using the java.nio.channels.FileChannel
class.
package org.kodejava.io;
import java.io.*;
import java.nio.channels.FileChannel;
public class FileCopy {
public static void main(String[] args) {
//// Define the source and target file
File source = new File("D:/Temp/source.txt");
File target = new File("D:/Temp/target.txt");
// Create the source and target channel
try (FileChannel sourceChannel = new FileInputStream(source).getChannel();
FileChannel targetChannel = new FileOutputStream(target).getChannel()) {
// Copy data from the source channel into the target channel
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I compile and execute a JDK preview features with Maven? - December 8, 2023
- How do I sum object property using Stream API? - December 7, 2023
- How do I iterate through date range in Java? - October 5, 2023
Hello my friend.
When I do it like your example it show to me that my path is not correct. When I check this error I found something about folder bin where Java JDK install. How can I resolve that?
Thanks.
Hi Hanan,
I need to see your code to know what was wrong with your code. Or at least I can see the error stack trace. So can you post the code?