Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy files from one Directory to another Directory. Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
copy files from one Directory to another Directory. 18-Oct-04
Category
Files Operation
Language
JBuilder All Versions
Views
405
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Darley, F. Joe
Reference URL:
			1   
2   import java.awt.*;
3   import java.awt.event.*;
4   import javax.swing.*;
5   import java.io.*;
6   
7    void copyFile(File sSourceFileName, File sTargetFileName)
8      //copies files from one location to another   
9      throws FileNotFoundException, IOException
10   {
11  FileInputStream aFileInputStream = new  FileInputStream(sSourceFileName);
12  FileOutputStream aFileOutputStream = new  FileOutputStream(sTargetFileName);
13     synchronized (aFileInputStream)
14     {
15       synchronized (aFileOutputStream)
16       {
17         byte [] buffer = new byte[256];
18         while (true)
19         {
20           int bytesread = aFileInputStream.read(buffer);
21           if (bytesread == -1)
22           {
23             break;
24           }
25           aFileOutputStream.write(buffer, 0, bytesread);
26         }
27       }
28     }
29     aFileInputStream.close();
30     aFileOutputStream.flush();
31     aFileOutputStream.close();
32   }
33  
34  // Copies all files under srcDir to dstDir.
35      // If dstDir does not exist, it will be created.
36      void copyDirectory(File srcDir, File dstDir) throws IOException {
37          if (srcDir.isDirectory()) {
38              if (!dstDir.exists()) {
39                  dstDir.mkdir();
40              }
41  
42              String[] children = srcDir.list();
43              for (int i=0; i<children.length; i++) {
44                  copyDirectory(new File(srcDir, children[i]),
45                                       new File(dstDir, children[i]));
46              }
47          } else {
48             
49              copyFile(srcDir, dstDir);
50          }
51      }
52  
53    void jButton2_actionPerformed(ActionEvent e)   {
54      
55    File sFile1 = new File("C:\\Test1\\");
56    File sFile2 = new File("C:\\Test2\\"); 
57    
58    //Call to copy files from directory to another
59    try {
60  
61      copyDirectory(sFile1 ,sFile2);
62    } catch (IOException e1) {
63        }
64  
65    }


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC