Checking and creating a directory

Source: Internet
Author: User

The

File class is not just a representation of an existing directory path, file, or filegroup. You can also use a file object to create a new directory, or even a complete directory path-if it does not already exist. It can also be used to understand the properties of a file (length, last modified date, read/write properties, and so on), to check whether a file object represents a document or a directory, and to delete a file, and so on. The following program shows you how to use the remaining methods of the file class:
 

: Makedirectories.java//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" + "Create S each path\n "+" usage:makedirectories-d path1 ... \ n "+" deletes each path\n "+" Usage:makedirectories-r PA
  Th1 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 () +" \ GetName: "+ f.getname () +" \ n getp  Arent: "+ f.getparent () +" \ 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 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); }  
  }
} ///:~

In Filedata (), you can see that various file survey methods have been applied to display information about a file or directory path.
The first method applied by main () is Renameto (), which can be used to rename (or move) a file to a completely new path (the path is determined by the parameter) and it belongs to the other file object. This also applies to any length of the directory.
If you test the above program, you can find yourself able to create any complexity of a directory path, because Mkdirs () will help us complete all the work. In Java 1.0, the-D flag Report directory is still present, although it has been deleted, but in Java 1.1 The directory is actually deleted.

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.