/** File operation class * java IO * File operation using File class **
If you want to develop all files and their contents, you should use java. io package completed * java. There are five core classes and one core interface in the io package*
Five core classes: File InputStream OutputStream Reader Writer*
A core interface: Serializable * * is in the whole javaa.io package
File class is the only class related to the operation of the file itself, but it does not involve the specific content of the file * the so-called file itself is the operation of file creation, deletion, etc*
If you want to use the File class, you first need to define a path to operate the File through the construction method provided by it * 1 construction method: public
File(String pathname) sets the full path * public File(File parent, String)
child) set the parent path and child file path. Use * * operation file * to create a file public boolean on Android
createNewFile() throws IOException * 1 if the directory cannot be accessed * 2 if the file name is duplicate now*
3. If the file name is wrong, an exception will be thrown * delete the file public boolean delete() * * judge whether the file exists
public boolean exists() * * the \ path separator is supported in the windows system, and the \ path separator is used in Linux/
-
- A constant public static final String separator is provided in the File class, indicating the separator * there will be a delay during the java.io operation, because the current problem is that the Java program is called through the JVM program*
The file processing function of the operating system performs file processing, so there will be a delay in the middle. * create a file containing a subdirectory * file = new
File("D:"+File.separator+"demo"+File.separator+"test.txt");
*If the demo directory does not exist, the system will think that the path at this time can not be used, and then a creation error will appear, * use the parent path that must be judged by some way * find the parent path: public String getParent() returns the null path name string of the parent of the abstract path name. If the path name is not named as the parent directory, it returns null.
The parent of an abstract pathname consists of the prefix of the pathname (if any) and each name in the name sequence of the pathname, except the last one. If the name sequence is empty, the pathname does not specify the parent directory.
public File getParentFile()
Returns the parent abstract pathname of this abstract pathname, or null if this pathname is not named as the parent directory.
The parent of an abstract pathname consists of the prefix of the pathname (if any) and each name in the name sequence of the pathname, except the last one. If the name sequence is empty, the pathname does not specify the parent directory.
mkdir() mkdir creates one level, mkdirs creates multi-level paths, and there are a series of operations to obtain content in the File class. 1
Get the file size public long length() and judge whether it is a file according to the bytes returned: public boolean
isFile() determines whether it is a directory: public boolean isDirectory() returns the last modification date
long lastModified() the whole process of obtaining files involves operations related to obtaining files, but does not contain the contents of the files*
- A constant public static final String separator is provided in the File class, indicating the separator * there will be a delay during the java.io operation, because the current problem is that the Java program is called through the JVM program*
*/
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; public class wenjiancaozuo { public static void main(String[] args) throws Exception { // Method stub automatically generated by TODO // File file = new File("D:"+File.separator+"test.txt"); // Set the path of the file // if(file.exists()) / / / file exists // { // file.delete(); // } // else // { // System.out.println(file.createNewFile()); // } // File file = new File("D:"+File.separator+"demo"+ // File.separator+"hello"+File.separator // +"nihao"+File.separator+"test.txt"); // Set the path of the file // if(!file.getParentFile().exists()) / / / the parent path does not exist now // { // ///Create directory // file.getParentFile().mkdirs(); Create parent path mkdir create one level // } // if(file.exists()) / / / file exists // { // file.delete(); // } // else // { // System.out.println(file.createNewFile()); // } File file = new File("D:"+File.separator+"demo"+ File.separator+"WIN_20201207_16_29_20_Pro.jpg"); if(file.exists()) { System.out.println("Is it a file"+file.isFile()); System.out.println("Is it a directory"+file.isDirectory()); System.out.println("file size"+file.length()); System.out.println("Last modified"+file.lastModified());///1607329760470 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). format(new Date(file.lastModified()))); } System.out.println(file.getPath()); } }