/**
* <p>title: Directory operations </p>
* <p>description: Demo column directory of files, and move a directory </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:Dir.java</p>
* @version 1.0
*/
import java.io.*;
public class dir{
/**
*<br> Method Description: Implementing Directory Listing
*<br> input Parameters:
*<br> return type:
*/
public string[] Dirlist (String pathName) {
try{
File path = null;
string[] filelist;
//If no directory is specified, the current directory is listed.
if (Pathname.equals (""))
path = new File (".");
Else
path = new File (pathName);
//Get catalog file list
if (path.isdirectory ())
filelist = Path.list ();
Else
return null;
return filelist;
}catch (Exception e) {
System.err.println (e);
return null;
}
}
/**
*<br> Method Description: Move a directory
*<br> Input Parameters: string Sou source directory, string obj new directory
*<br> return type:
*/
public boolean Dirmove (string sou, string obj) {
try{
//Check whether the source file exists
Boolean sexists = (new File (SOU)). Isdirectory ();
if (!sexists) return false;
Boolean oexists = (new File (obj)). Isdirectory ();
//target directory does not exist, create a
if (!oexists) {
(new File (obj)). Mkdirs ();
}
File File = new file (SOU);
file dir = new file (obj);
//Mobile directory
Boolean success = File.renameto (new file (dir, File.getname ()));
if (!success) {
System.out.println ("Copy error!");
return false;
}
else return true;
}catch (Exception e) {
System.out.println (e);
return false;
}
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
dir d = new Dir ();
if (args.length==0) {
return;
}else{
String cmd = args[0];
if (cmd.equals ("list")) {
if (args.length!=2) return;
string[] stemp = d.dirlist (args[1));
for (int i=0;i<stemp.length;i++)
System.out.println (Stemp[i]);
}else if (cmd.equals ("move")) {
if (args.length!=3) return;
D.dirmove (args[1],args[2]);
}
}
}
}