This should give you enough hits to code what you need.
Good Luck.
//: c11:MakeDirectories.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Demonstrates the use of the File class to
// create directories and manipulate files.
import java.io.*;
public class MakeDirectories {
private final static String usage =
"Usage:MakeDirectories path1 ...\n" +
"Creates each path\n" +
"Usage:MakeDirectories -d path1 ...\n" +
"Deletes each path\n" +
"Usage:MakeDirectories -r path1 path2\n" +
"Renames from path1 to path2\n";
private static void usage() {
System.err.println(usage);
System.exit(1);
}
private static void fileData(File f) {
System.out.println(
"Absolute path: " + f.getAbsolutePath() +
"\n Can read: " + f.canRead() +
"\n Can write: " + f.canWrite() +
"\n getName: " + f.getName() +
"\n getParent: " + f.getParent() +
"\n getPath: " + f.getPath() +
"\n length: " + f.length() +
"\n lastModified: " + f.lastModified());
if(f.isFile())
System.out.println("it's a file");
else if(f.isDirectory())
System.out.println("it's a directory");
}
public static void main(String[] args) {
if(args.length < 1) usage();
if(args[0].equals("-r")) {
if(args.length != 3) usage();
File
old = new File(args[1]),
rname = new File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
return; // Exit main
}
int count = 0;
boolean del = false;
if(args[0].equals("-d")) {
count++;
del = true;
}
for( ; count < args.length; count++) {
File f = new File(args[count]);
if(f.exists()) {
System.out.println(f + " exists");
if(del) {
System.out.println("deleting..." + f);
f.delete();
}
}
else { // Doesn't exist
if(!del) {
f.mkdirs();
System.out.println("created " + f);
}
}
fileData(f);
}
}
} ///:~
-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet API
Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of karthikeyan
Sent: Sunday, December 30, 2001 11:32 AM
To: [EMAIL PROTECTED]
Subject: Java Question
Importance: High
Hi All,
I know its wrong to ask java question in servlet-interest but still i
doing this because it is urgent. I have a problem in parsing a string. It
is like this:
I am developing a file search program in java, it is just a part of my
module. It should do some thing like this if you give file name as
parameter to this program it should search through your hard disk and give
you all information regarding the file for example - file name, file size,
date of last modification, file belongs to which user etc.
What i did right now is, i used Process and Runtime class to run my
command "dir filename /s" which of course returned me the output of the
command. It is something like this :
C:\>dir whenever.mp3 /s
Volume in drive C has no label.
Volume Serial Number is 2143-080A
Directory of C:\
12/30/01 07:54p 3,174,400 whenever.mp3
1 File(s) 3,174,400 bytes
Directory of C:\mp3
12/30/01 07:54p 3,174,400 whenever.mp3
1 File(s) 3,174,400 bytes
Total Files Listed:
2 File(s) 6,348,800 bytes
959,447,040 bytes free
C:\>
I got the above output in a string called [ cmdOutput ]. What i really
want is to get only [ c:\ ] and [ c:\mp3 ] and put them in a Vector. So now
this vector has only 2 elements - c:\ and c:\mp3.
What i will do afterwards is i will join c:\ with the filename [
whenever.mp3 ] pass it to File class and get all the information, same
applies to c:\mp3.
The above is just a sample but actual file search can return a lot of
Directory name.
You can also suggest me some other way of retrieving file information and
storing it in an Vector or Hashtable.
Looking forward for yours early response.
With Regards,
karthikeyan.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html