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)