Java’da Bir Dosya Kopyalamanın Kolay ve Kısa Yolu

Java’ da herhangi bir dosyayı yüksek performans ile kopyalamak için aşağıdaki kod bloğunu kullanabilirsiniz. Tabi öncelikle import anahtar sözcüğü ile aşağıdaki sınıfları uygulamanıza dahil etmeniz gerekmektedir.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public static void copyFile(File sourceFile, File destFile) throws IOException {
 if(!destFile.exists()) {
  destFile.createNewFile();
 }

 FileChannel source = null;
 FileChannel destination = null;
 try {
  source = new FileInputStream(sourceFile).getChannel();
  destination = new FileOutputStream(destFile).getChannel();
  destination.transferFrom(source, 0, source.size());
 }
 finally {
  if(source != null) {
   source.close();
  }
  if(destination != null) {
   destination.close();
  }
 }
} 
Advertisements

About kaanmutlu

Software Developer - Computer Engineer from Istanbul, Turkey
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s