نسخ الملفات في جافا عملية شائعة للغاية. ولكن فئة java.io.File ليس لديها أي طريقة اختصار لنسخ ملف من مصدر إلى وجهة. هنا سنتعلم عن أربع طرق مختلفة يمكننا نسخ الملف في جافا.
نسخ الملف في جافا
- نسخ الملف في جافا – تيار
هذه هي الطريقة التقليدية لنسخ الملف في جافا. هنا ننشئ ملفين – المصدر والوجهة. ثم ننشئ InputStream من المصدر ونكتبه إلى ملف الوجهة باستخدام OutputStream لعملية نسخ الملف في جافا. هنا هو الأسلوب الذي يمكن استخدامه لنسخ الملف في جافا باستخدام التيارات.
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
- نسخ الملف في جافا – java.nio.channels.FileChannel
تم تقديم فئات Java NIO في Java 1.4 ويمكن استخدام FileChannel لنسخ الملف في جافا. وفقًا لوثائق transferFrom() الطريقة ، من المفترض أن طريقة نسخ الملف هذه تكون أسرع من استخدام التيارات لنسخ الملفات في جافا. هنا هو الأسلوب الذي يمكن استخدامه لنسخ ملف باستخدام FileChannel في جافا.
private static void copyFileUsingChannel(File source, File dest) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{
sourceChannel.close();
destChannel.close();
}
}
- نسخ الملف في جافا – Apache Commons IO FileUtils
تستطيع استخدام Apache Commons IO FileUtils.copyFile(File srcFile, File destFile) لنسخ الملف في جافا. إذا كنت قد استخدمت بالفعل Apache Commons IO في مشروعك، فإنه من المنطقي استخدام هذا الكود لبساطته. يستخدم داخليًا Java NIO FileChannel، لذا يمكنك تجنب هذا الطريقة الوسيطة إذا لم تكن قد استخدمتها بالفعل لوظائف أخرى. إليك الطريقة لاستخدام Apache Commons IO لعملية نسخ الملف في جافا.
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
- نسخ ملف جافا – فئة Files
إذا كنت تعمل على جافا 7 أو أحدث، يمكنك استخدام فئة Files وطريقة copy() لنسخ الملف في جافا. يستخدم ذلك موفري نظام الملفات لنسخ الملفات.
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
الآن لمعرفة أيها الأسرع، كتبت فئة اختبار ونفذت الطرق أعلاه واحدة تلو الأخرى لنسخ ملف بحجم 1 جيجابايت. في كل مرة، استخدمت ملفات مختلفة لتجنب أي فائدة للطرق لاحقًا بسبب التخزين المؤقت.
package com.journaldev.files;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
public class JavaCopyFile {
public static void main(String[] args) throws InterruptedException, IOException {
File source = new File("/Users/pankaj/tmp/source.avi");
File dest = new File("/Users/pankaj/tmp/dest.avi");
// نسخ الملف بالطريقة التقليدية باستخدام التدفق
long start = System.nanoTime();
copyFileUsingStream(source, dest);
System.out.println("Time taken by Stream Copy = "+(System.nanoTime()-start));
// نسخ الملفات باستخدام java.nio FileChannel
source = new File("/Users/pankaj/tmp/sourceChannel.avi");
dest = new File("/Users/pankaj/tmp/destChannel.avi");
start = System.nanoTime();
copyFileUsingChannel(source, dest);
System.out.println("Time taken by Channel Copy = "+(System.nanoTime()-start));
// نسخ الملفات باستخدام apache commons io
source = new File("/Users/pankaj/tmp/sourceApache.avi");
dest = new File("/Users/pankaj/tmp/destApache.avi");
start = System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
System.out.println("Time taken by Apache Commons IO Copy = "+(System.nanoTime()-start));
// باستخدام فئة Java 7 Files
source = new File("/Users/pankaj/tmp/sourceJava7.avi");
dest = new File("/Users/pankaj/tmp/destJava7.avi");
start = System.nanoTime();
copyFileUsingJava7Files(source, dest);
System.out.println("Time taken by Java7 Files Copy = "+(System.nanoTime()-start));
}
}
هذا هو إخراج البرنامج أعلاه، يرجى ملاحظة أنني قمت بتعليق الشيفرة أعلاه للتأكد من أن يتم استخدام طريقة واحدة فقط في كل مرة لعملية نسخ ملف جافا.
Time taken by Stream Copy = 44582575000
Time taken by Channel Copy = 104138195000
Time taken by Apache Commons IO Copy = 108396714000
Time taken by Java7 Files Copy = 89061578000
من الإخراج، يظهر بوضوح أن Stream Copy هو أفضل طريقة لنسخ الملف في لغة البرمجة جافا. ولكنها اختبار أساسي جدًا. إذا كنت تعمل على مشروع يتطلب أداءًا مكثفًا، فيجب عليك أن تجرب طرقًا مختلفة لنسخ الملف في جافا وتسجل الأوقات لتحديد أفضل نهج لمشروعك. يجب أيضًا أن تتلاعب بطرق مختلفة لنسخ الملفات في جافا بناءً على حجم الملف الذي تعمل عليه بشكل متوسط. لقد قمت بإنشاء فيديو على YouTube يشرح 4 طرق لنسخ الملف في جافا، يمكنك مشاهدته للمزيد من المعلومات. https://www.youtube.com/watch?v=op6tgG95zek
Source:
https://www.digitalocean.com/community/tutorials/java-copy-file