Magazine
 
Tips & Tricks
 

SuperClass without entering into constructor of SubClass. Here also the instance variables of SuperClass are initialized and then super class’s constructor is called then control comes back to call the constructor of SubClass.

SubClass.java:

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);
System.out.println(“File or Directory
copied.”);
}
public void copyDirectory(File srcPath, File
dstPath) throws IOException
{
if (srcPath.isDirectory())
{
if (!dstPath.exists())
{
dstPath.mkdirs();
}
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);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
}

Output:

If the user enter valid path of the file or directory then it shows a message that it has been copied to the proper destination.

C:\Program Files\Java\jdk1.6.0\bin>javac
CopyDirectory.java
C:\Program Files\Java\jdk1.6.0\bin>java
CopyDirectory
Enter the source directory or file name :
C:\JavaJazzUp
Enter the destination directory or file name :
D:\RoseIndia\JavaJazzUp
File or Directory copied.
C:\Program Files\Java\jdk1.6.0\bin>

When the user enters wrong source file or directory name to be copied that does not exist then it shows a message that the source file or directory does not exist.

C:\Program Files\Java\jdk1.6.0\bin>java
CopyDirectory
Enter the source directory or file name :
C:\JavaJazzUpNew
Enter the destination directory or file name :
D:\JavaJazzUp
File or directory does not exist.
C:\Program Files\Java\jdk1.6.0\bin>

Oct 2007 | Java Jazz Up | 75
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF