Package Fengke.filedemo;
Import Java.io.File;
Import java.io.IOException;
/**
* Basic use of java.io.File and case-sensitive issues in Windows
* @author Feng Ke
* Note: File creation exception, default path and absolute path problem; multi-level directory creation
* Master three kinds of construction methods; cannot be used for access to file content.
*
*/
public class Filedemo {
/* The file class is used only for information (name, size, etc.) that represent files (directories) and cannot be used for access to file content
* Three construction methods provided by the file class (emphasis)
* 1) File (String pathname)
* Example: File F1=new file ("FileTest1.txt");
* Note: Creating a File Object F1,f1 refers to a file created under the current directory FileTest1.txt
* Default Path problem
* 2) File (string parent, String child)
* Example: File F2=new file ("D:\\dir1", "FileTest2.txt");
* Note: The D:\\dir1 directory must exist beforehand, otherwise the exception
* 3) file (file parent, String child)
* Example: File F4=new file ("\\dir3"); Directory
* File F5=new file (F4, "FileTest5.txt");
* If \\dir3 directory does not exist use F4.mkdir () to create first
*file provides the method:
* Method of Creation:
* 1.boolean CreateNewFile () does not exist return true existence returns false
* 2.boolean mkdir () Create directory
* 3.boolean mkdirs () Create multi-level catalogs
* Delete method:
* 1.boolean Delete ()
* 2.boolean deleteonexit () file deleted after use is complete
* Method of judgment:
* 1.boolean CanExecute () Determine if the file is executable
* 2.boolean canRead () Determine if the file is readable
* 3.boolean canWrite () Determine if the file is writable
* 4.boolean exists () determine if the file exists
* 5.boolean isdirectory () determine if the directory
* 6.boolean isfile () Determine if the file is
* 7.boolean Ishidden () to determine whether to hide
* 8.boolean Isabsolute () determine if an absolute path file does not exist or can be judged
* 9.boolean Isformfield ()
* Normal text form field, or a file form field, returns True if it is a normal form field, otherwise false.
* Therefore, you can use this method to determine whether the form fields are normal or file upload.
*
* Get Method:
* 1. String getName ()//Get the file name in the Upload field
* Note: The file name obtained in IE or Firefox is not the same, IE is the absolute path, Firefox is just the file name.
* 2. String GetPath ()//default path: Relative path when created, absolute path when created
* 3. String GetAbsolutePath ()//absolute path
* 4. String Getgetcanonicalpath ()//full path
* If the construction is the full path, then the direct return to the full path
* If you try to construct a relative path, return the path of the current directory + the path to construct the file
* 4. String getParent ()//If no parent directory returns null
* 5. Long lastmodified ()//Gets the last modified time
* 6. Long Length ()
* 7. Boolean Renameto (File f)
* 8. File[] Liseroots ()//Get Machine drive letter
* 9. String[] List ()
* 10.string[] List (filenamefilter filter)
* 11.String getfieldname ()//Returns the value of the form label Name property. <input type= "text" name= "column"/> value
*
*/
public static void Main (string[] args) throws IOException {
Understanding the condition of the constructor check help
File File = new file ("E:\\javaio\\fengke");
System.out.println (File.exists ());
if (!file.exists ())
File.mkdirs (); File.mkdir ()
Else
File.delete ();
The file or directory has not been created yet
System.out.println ("********** created File,file2,file3 before ********");
is a directory if the directory returns true if it is not a directory or directory does not exist return is false
System.out.println ("There is a directory e:\\javaio\\fengke=====" +file.isdirectory ());
Whether it is a file
System.out.println ("is a file e:\\javaio\\fengke=====" +file.isfile ());
File File2 = new file ("E:\\javaio\\ diary 1.txt");
File File2 = new file ("E:\\javaio", "Diary 1.txt");
Use of paths
File File3 = new file ("Diary 2.txt");
if (!file2.exists ())
try {
File2.createnewfile ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Else
File2.delete ();
if (!file3.exists ())
try {
File3.createnewfile ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Else
File3.delete ();
APIs for commonly used file objects
System.out.println ("************* path Problem **************");
SYSTEM.OUT.PRINTLN ("Output Directory file = new file (' E:\\javaio\\fengke '):" +file); the contents of//file.tostring ()
System.out.println ("Directory file = absolute path to new file (' E:\\javaio\\fengke '):" +file.getabsolutepath ());
System.out.println ("Directory file = The default path for new file (' E:\\javaio\\fengke '):" +file.getpath ());
System.out.println ("File File3 = default path for ' new ' (' Journal 2.txt '):" +file3.getpath ());
System.out.println ("************* File and directory Operation **************");
SYSTEM.OUT.PRINTLN ("Output directory Name:" +file.getname ());
SYSTEM.OUT.PRINTLN ("Output file name:" +file2.getname ());
SYSTEM.OUT.PRINTLN ("Parent Directory of Output Directory" +file.getparent ());
SYSTEM.OUT.PRINTLN ("Parent directory of output file" +file2.getparent ());
SYSTEM.OUT.PRINTLN ("absolute path to the parent directory of the output file" +file.getparentfile (). GetAbsolutePath ());
System.out.println ("File2 file is hidden:" +file2.ishidden ());
/*
*
* window is case insensitive, so when Windows is not a file, the resulting path is followed by the path entered.
* But when the file is present, it will be displayed in the actual situation.
* This is a different reason for creating files and deleting files. Folders and files are similar.
*/
About case issues in Windows
System.out.println ("================windows in case of ==============");
File File_windows = new file ("E:\\text.txt");
System.out.println (File_windows.getcanonicalpath ());
When the user does not have file Text.txt, the output is E:\Text.txt
When the user has created the file Text.txt, the output is E:\text.txt
}
}
Basic usage of java.io.File and about case-sensitive issues in Windows