import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class SalinanChannel {
    public static void main(String[] args) throws IOException {
        Path sumber = Path.of("kutipan.txt");
        Path tujuan = Path.of("salinan.txt");
        long total = 0;

        try (FileChannel masuk = FileChannel.open(sumber, StandardOpenOption.READ);
             FileChannel keluar = FileChannel.open(tujuan,
                     StandardOpenOption.CREATE, StandardOpenOption.WRITE,
                     StandardOpenOption.TRUNCATE_EXISTING)) {
            ByteBuffer penampung = ByteBuffer.allocate(4);
            while (masuk.read(penampung) != -1) {
                penampung.flip();
                total += keluar.write(penampung);
                penampung.clear();
            }
        }

        System.out.println("total byte  : " + total);
        System.out.println("ukuran sama : "
                + (Files.size(sumber) == Files.size(tujuan)));
        System.out.println("isi sama    : "
                + Files.readString(tujuan, StandardCharsets.UTF_8)
                        .equals(Files.readString(sumber, StandardCharsets.UTF_8)));
    }
}
