Java basic file Class (11)

Source: Internet
Author: User

The Java.io.File class represents the System file name (path and file name).

Java.io.IOException: Input Output file stream exception handling class.

Common construction methods for the file class:

Public file (String pathname): Creates a file object with pathname as the path, and if pathname is a relative path, the default current path is stored in the system Properties User.dir.

Public file (String parent,string child): The parent path is parented, and children creates a File object for the sub-path.

The static property of file, string separator, stores the path delimiter for the current system.

File Class Common methods:

The file object can access the properties of the files

public boolean canRead ();

public boolean canWrite ();

public boolean exists ();

public boolean isdirectory ();

public boolean isfile ();

public boolean Ishidden ();

Public long lastmodified ();

public long length ();

Public String getName ();

Public String GetPath ();

Create an empty file or directory from the file object (in the case where the object refers to a document or directory that does not exist):

public Boolean CreateNewFile () throws IOException

public Boolean Delete ()

public boolean mkdir ()

public boolean mkdirs ()//Create a series of directories in the path

Example one:

Import Java.io.File;

Import java.io.IOException;

public class Myfileclass {

public static void Main (string[] args) {

String Separator=file.separator;

SYSTEM.OUT.PRINTLN (separator);

String filename= "MyFile.txt";

String directory= "Mydir1" +separator+ "Mydir2";

File F=new file (directory,filename);

if (f.exists ()) {

System.out.println (F.getparentfile ());

SYSTEM.OUT.PRINTLN ("FileName:" +f.getabsolutepath ());

SYSTEM.OUT.PRINTLN ("File Size:" +f.length ());

}else{

F.getparentfile (). Mkdirs ();

try{

F.createnewfile ();

}catch (IOException e) {

E.printstacktrace ();

}

}

}

}


This article is from the "one step, one step" blog, please be sure to keep this source http://summerflowers.blog.51cto.com/5202033/1926412

Java basic file Class (11)

Related Article

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.