Saturday, February 5, 2011

كيفية نسخ ملف في جافا

حتى جافا 6 لا توجد دالة واحدة لنسخ الملفات. لنسخ الملف عليك قراءته في وسيط تخزين buffer ثم اعادة كتابته

 private Boolean copyfile(String source, String destination)  
 {  
   try  
   {  
     InputStream is = new FileInputStream(new File ("/u01/" + source));  
     OutputStream os = new FileOutputStream(new File ("/u01/" + destination))  
     byte[] buf = new byte[10124];  
     int len = 0;  
     while (0 < (len = is.read(buf)))  
     {  
       os.write(buf, 0, len);  
     }  
     is.close();  
     os.flush();  
     os.close();  
     return true;  
   }  
   catch(Exception e)  
   {  
     e.printStackTrace();  
     return false;  
   }  


في جافا 7
 new File(source).toPath().copyTo(new File (destination).toPath(), true, true);  

No comments:

Post a Comment