1. Create and delete folders
- Create folder: Public boolean CreateNewFile () throws IOException. The file path does not exist or the file is inaccessible and throws a IOException exception
- Delete folder: public boolean Delete ()
- Determine if a file exists: public boolean exists()
ImportJava.util.*; ImportJava.io.*; Public classhello{ Public Static voidMain (string[] args)throwsException {File f=NewFile ("C:" + file.separator + "D" + file.separator + "code" + File.separator + "resource" + File.separator + "Test.txt"); if(F.exists ()) {f.delete (); } Else{f.createnewfile (); } }}
2. Exists () can determine if the file exists, but when we create a new file, we should also ensure that its parent folder is present.
- Gets the path of the parent folder in the form of a string. Public String getParent()
- Gets the parent folder as a file instance. Public File getparentfile()
- Creates the specified folder, at which point all parent folders of the file should be present. public boolean mkdir()
- Creates the specified folder, all folders in that path, and if it does not exist, it is created uniformly. public boolean mkdirs()
ImportJava.util.*; ImportJava.io.*; Public classhello{ Public Static voidMain (string[] args)throwsException {File f=NewFile ("C:" + file.separator + "D" + file.separator + "Hello" + file.separator + "abc" + File.separator + "Test.txt"); String ParentFolderPath=f.getparent (); File ParentFolder=F.getparentfile (); System.out.println ("The parent path of the file is:" +ParentFolderPath); if(!parentfolder.exists ()) {//Create all parent folders for a fileSystem.out.println ("The parent path does not exist, now creates one for it.") "); Parentfolder.mkdirs (); } Else{System.out.println ("The parent path already exists."); } if(F.exists ())//determine if a file exists{//if present, deleteF.delete (); } Else {//If it does not exist, createF.createnewfile (); } }}
3. Get some information about a file
- or fetch the length of the file public long length()
- Determines whether a file is public boolean isfile()
- Determine if a folder is public boolean isdirectory()
- Last modified: public long lastmodified()
ImportJava.util.*; ImportJava.io.*;Importjava.math.*;Importjava.text.*; Public classhello{ Public Static voidMain (string[] args)throwsException {File f=NewFile ("C:" + file.separator + "F" + file.separator + "JAVA" + file.separator + "Javaweb" + file.separator + "Java_ee_javad Ocs.zip "); LongFilelength =f.length (); LongLastmodifiedtime =f.lastmodified (); System.out.println ("File Size:" +NewBigDecimal (Filelength). Divide (NewBigDecimal (1024*1024), 2,roundingmode.half_down) + "M"); System.out.println ("Last modified:" +NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS"). Format (lastmodifiedtime)); System.out.println ("is the file:" +f.isfile ()); System.out.println ("is a folder:" +f.isdirectory ()); }}
4. List all files in a folder
- Get the full path to all files in the current folder: Lpublic string[] list()
- Get all files in the current folder: public file[] listfiles()
ImportJava.util.*; ImportJava.io.*;Importjava.math.*;Importjava.text.*; Public classhello{ Public Static voidMain (string[] args)throwsException {File f=NewFile ("C:" + file.separator + "F" + file.separator + "JAVA" + file.separator + "Javaweb"); if(F.isdirectory ()) {file[] files=F.listfiles (); for(File item:files) {System.out.println (Item.getname ()); } System.out.println ("==================="); String[] Filesname=f.list (); for(String item:filesname) {System.out.println (item); } } }}
Use of the file class in Java