Methods for creating and deleting files/directories commonly used File classes
1.boolean exists(): determines whether a file or directory exists
2.boolean createNewFile(): Create a new file
3.boolean delete(): delete the file
4.boolean mkdirs(): Recursively create multi-level directories
5.File getParentFile(): Get the previous directory
1. Create/delete file
try { //Create a new file in the demo directory of D disk: test.txt File file = new File("D://demo//test.txt"); //If the file exists, delete, do not exist, create if(!file.exists()) { //Don't exist, create file.createNewFile(); }else { //Exist, delete file.delete(); }} catch (Exception e) { e.printStackTrace();}•Create a directory
try { // Create a new directory "/a/b/c" in the demo directory of D disk File file = new File("D://demo//a//b//c"); if(!file.getParentFile().exists()) { //The superior directory does not exist, create the superior directory file.getParentFile().mkdirs(); } file.mkdirs();} catch (Exception e) { e.printStackTrace();}File operation
1.String getName(): Get the file name
2.long length(): get the file size and return byte unit
3.File getParentFile(): Get the File object of the previous directory
4.String getParent(): Get the path of the previous directory
5.long lastModified(): last modified time
6.boolean isFile(): Is it a file?
try { //Use the constructor to determine the file to operate File file = new File("D:"+File.separator+"demo"+File.separator+"NOKIApptx"); //Get the file name String name = file.getName(); System.out.println("File name: "+name); //Get the file suffix System.out.println("File suffix: "+name.substring(name.lastIndexOf(".")+1)); //Get the file size long length = file.length();//tyte double size = (double)length / 1024;//byte->kb DecimalFormat format = new DecimalFormat("0.00");//Retain two decimal places System.out.println("File size: "+format.format(size)); //Get the upper directory System.out.println(file.getParentFile()); System.out.println(file.getParent()); //Get the last modification time long lastModified = file.lastModified(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(lastModified); System.out.println(dateFormat.format(date)); //Is it a file System.out.println(file.isFile());} catch (Exception e) { e.printStackTrace();}Directory operation
1.String[] list(): Get the String representation of all files in the directory
2.File[] listFiles(): Get the File representation of all files in the directory
3.boolean isDirectory(): Is it a directory
try { File file = new File("D:"+File.separator+"demo"); String[] list = file.list(); for (String s : list) { System.out.println(s); } System.out.println("-------------------"); //Is it a directory System.out.println(file.isDirectory()); //Statistics the number of subdirectories and files in a directory File[] listFiles = file.listFiles(); int fileCount = 0;//Number of files int directoryCount = 0;//Number of directories for (File f : listFiles) { if(f.isFile()) { fileCount++; }else { directoryCount++; } } System.out.println("File: "+fileCount+"; directory: "+directoryCount+");} catch (Exception e) { e.printStackTrace();}Recursive output directory
The program calls itself
//Recursively output directory information: directory subdirectory, file public static void print(File file) { //Judge whether it is a directory if(file.isDirectory()) { //Are a directory, get the subdirectory and file File[] listFiles = file.listFiles(); if(listFiles != null) { for (File f : listFiles) { print(f); } } } System.out.println(file);}Summarize
The above is the method of creating and deleting files and directories in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!