this is the correct program,
November 6, 2008 at 11:17 PM
import java.io.*; import java.nio.*;
public class file2{ public static void main(String[] args) throws IOException {
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); copyDirectory(src, dst); }
public static 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[i]), new File(dstPath, files[i])); } } 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."); } }
View All Comments
| View Tutorial