D:\Hany\HalalFarms\HalalCentersHaulersJavaProject\HalalCentersHauler\src\java\utils\CopyFile.java
 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 
 6 package utils;
 7 
 8 import java.io.File;
 9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 
16 /**
17  *
18  * @author sameldin
19  */
20 public class CopyFile
21 {
22         public CopyFile()
23         {
24         }
25     /*
26      *
27      */
28     public static void copyfile(String srFile, String dtFile)
29     {
30             try
31             {
32               File f1 = new File(srFile);
33               File f2 = new File(dtFile);
34               InputStream in = new FileInputStream(f1);
35               //For Append the file.
36 //            OutputStream out = new FileOutputStream(f2,true);
37               //For Overwrite the file.
38               OutputStream out = new FileOutputStream(f2);
39               byte[] buf = new byte[1024];
40               int len;
41               while ((len = in.read(buf)) > 0){
42                 out.write(buf, 0, len);
43               }
44               in.close();
45               out.close();
46               System.out.println("File copied.");
47             }
48             catch(FileNotFoundException ex){
49               System.out.println(ex.getMessage() + " in the specified directory.");
50               System.exit(0);
51             }
52             catch(IOException e){
53               System.out.println(e.getMessage());
54             }
55     }
56     /*
57      *
58      */
59     public static void copy(File src, File dst) throws IOException
60     {
61                 InputStream in = new FileInputStream(src);
62                 OutputStream out = new FileOutputStream(dst);
63 
64                 // Transfer bytes from in to out
65                 byte[] buf = new byte[1024];
66                 int len;
67                 while ((len = in.read(buf)) > 0) {
68                     out.write(buf, 0, len);
69                 }
70                 in.close();
71                 out.close();
72     }
73 
74 }
75