Welcome Guest of Netspec |
Sign in
|
Sign up
Tell me more about:
Home
Service
Contact us
Forum
Portfolio
FAQs
Payment
Welcome to Netspec
E-commerce | Web design | Software design | Server setup | Web hosting | Free classifieds
Feel free to contact us if you need any help!
E-commerce
|
Web server
|
Web design
|
Software design
|
Technical support
Have a look our work samples, see what we can do for you.
View frequent asked questions first if you are a new user. For more info, you can check forum or search our knowledge database.
Monthly Payment
|
One off payment
You are here:
Forum Home
>>
Post new topic
URL:
1 - 2 [ 2]
Master:
# 1
Copy whole file directory with java
exp
Online
no
State
Grouping
Member
Class
class4
Score
348
Wealth
219
Posts
269
Login
11:49:09
the source code shown below:
import java.io.*; public class CopyDirectory{ public static void main(String[] args) throws IOException{ CopyDirectory cd = new CopyDirectory(); BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter the source directory or file name : "); String source = in.readLine(); File src = new File(source); System.out.println("Enter the destination directory or file name : "); String destination = in.readLine(); File dst = new File(destination); cd.copyDirectory(src, dst); } public void copyDirectory(File srcPath, File dstPath) throws IOException{ if (srcPath.isDirectory()){ if (!dstPath.exists()){ dstPath.mkdir(); } String files[] = srcPath.list(); for(int i = 0; i < files.length; i++){ copyDirectory(new File(srcPath, files
), new File(dstPath, files
)); } } else{ if(!srcPath.exists()){ System.out.println("File or directory does not exist."); System.exit(0); } else { InputStream in = new FileInputStream(srcPath); OutputStream out = new FileOutputStream(dstPath); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } } System.out.println("Directory copied."); } }
Hits: 276, Date: 2010-04-09 03:23:12
[Agree]
( 7
)
[Against]
( 9
)
Quote
|
Edit
# 2
there is a recursive method copyDirectory()
exp
Online
no
State
Grouping
Member
Class
class4
Score
348
Wealth
219
Posts
269
Login
11:49:09
if it s file directory, list its files, if it is file, then copy it.
Hits: 275, Date: 2010-04-09 03:24:48
[Agree]
( 8
)
[Against]
( 9
)
Quote
|
Edit
1 - 2 [ 2]
Reply: Copy whole file directory with java
*First line:
More content:
Tags:
Hint
: You cannot post here before login
Login
|
Register
nothing