SORU
18 Kasım 2008, Salı


Hareket / Java ile Dosya Kopyalama İşlemleri

Dosyaları taşıma/kopyalama/klasörleri gibi ortak dosya işlemleri işleyen standart bir Java kütüphanesi var mı?

CEVAP
26 Mayıs 2009, Salı


1 ** işlemleri ile bunu yapmak için:

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();

        // previous code: destination.transferFrom(source, 0, source.size());
        // to avoid infinite loops, should be:
        long count = 0;
        long size = source.size();              
        while((count  = destination.transferFrom(source, count, size-count))<size);
    }
    finally {
        if(source != null) {
            source.close();
        }
        if(destination != null) {
            destination.close();
        }
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Matt Davis

    Matt Davis

    4 ŞUBAT 2006
  • The Weavers of Eternity Paracord Tutorials

    The Weavers

    1 Ocak 2014
  • Tips On Linux

    Tips On Linu

    26 Temmuz 2008