1, create a file
File File = new file (path); File.createnewfile (); If the path does not exist, it throws an exception
File.mkdir ();//If the path does not exist, returns false, the directory is not created
File.mkdirs ();//If it is .../xx.txt, the Xx.txt directory is created
Note: If path does not have a write letter path, it will be created under the project path
2. deleting files
Delete ();
Note: Delete in Java does not go to recycle Bin
Files/folders cannot be included in folders when deleting a folder
3, renaming
File.renameto (File dest)//
4, judging
File.isdirectory ();
File.isfile ();
File.exists ();
File.canread ();
File.canwrite ();
File.ishidden ();
5, get
File.getabsolutepath ();
File.getpath ();
File.getname ();
Long length = File.length ();//byte number length/1024/1024 = M
Loong time = file.lastmodified ();//The millisecond value of the last modification
Note: How to convert a millisecond value to a date
Date d = new date (time);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:MM:SS")
String lastmodifiedtime = Sdf.format (d);
String[] List ();//array of names
File[] Listfiles ();//file Array (File.getname get name)
Example:
Determine if there is a. jpg in the directory, and if so, the output
static void GetJpgName2 (String path) {
File File = new file (path);
string[] Namearr = file.list (new FilenameFilter () {
@Override
Public Boolean Accept (File dir, String name) {
return new File (dir, name). Isfile () && name.endswith (". jpg");
}
});
for (String Name:namearr) {System.out.println (name);}}
Java Foundation--file