[JAVA100 example]036, directory operations

Source: Internet
Author: User

/**


* <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]);


   }





  }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.