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 use Proxy class to configure HTTP and SOCKS proxies in Java? - March 27, 2025
- How do I retrieve network interface information using NetworkInterface in Java? - March 26, 2025
- How do I work with InetAddress to resolve IP addresses in Java? - March 25, 2025
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?