Java determines whether a file or folder exists, does not exist, and does not exist, and does not exist, and does not exist.
In Java, to determine whether a file or folder exists and perform creation or deletion operations based on the judgment results, you can use classes in classes or packages. The following is a sample code that uses these two classes to implement this function separately.
Use class
import ; import ; public class FileOrDirectoryOperations { public static void main(String[] args) { String path = "path/to/your/file_or_directory"; File fileOrDirectory = new File(path); if (()) { // If it exists, delete (whether it is a file or a folder) if (()) { ("File or folder has been deleted."); } else { ("File or folder deletion failed."); } } else { // If it does not exist, create a folder (note: no file will be created here, only folders will be created) // If you want to create a file, please use() instead of mkdir() if (()) { // Or use mkdirs() to create multi-level directories ("Folder created."); } else { ("Folder creation failed."); } } } }
Note: There is a problem with the above code, that is, it assumes that what you want to create is a folder instead of a file. If you want to create a file, you should use the createNewFile() method. However, since the question requires the judgment of "file or folder", we use mkdir() to create folders as an example. If you need to create a file, please modify the code accordingly.
User package
import ; import ; import ; import ; public class FileOrDirectoryOperationsNIO { public static void main(String[] args) { String pathString = "path/to/your/file_or_directory"; Path path = (pathString); if ((path)) { // If it exists, delete (whether it is a file or a folder) try { // Note: () If the path is a directory and is not empty, DirectoryNotEmptyException will be thrown // If you want to delete non-empty directories, you need to use () to traverse and delete everything in the directory (path); ("File or folder has been deleted."); } catch (IOException e) { //Catch and handle exceptions, such as DirectoryNotEmptyException ("File or folder deletion failed:" + ()); if (e instanceof ) { ("The directory is not empty and cannot be deleted."); // You can choose to recursively delete the directory contents } } } else { // If it does not exist, try creating (here assumes that what you want to create is a folder) // If you want to create a file, please use (path) try { // Create a single-level directory. If the parent directory does not exist, a NoSuchFileException will be thrown // If you want to create multi-level directories, please use (path) (path); ("Folder created."); } catch (IOException e) { ("Folder creation failed:" + ()); } } } }
Notice:
() Method If you try to delete a non-empty directory, a DirectoryNotEmptyException will be thrown. If you want to delete a non-empty directory, you need to first iterate over and delete all files and subdirectories in the directory.
The () method will only create single-level directories. If the parent directory does not exist, it throws a NoSuchFileException. If you want to create multi-level directories, use the () method.
In the above code, I assume that what you want to create is a folder instead of a file. If you want to create a file, use the (path) method instead of (path).
Please modify the above code according to your specific needs. If you need to handle both the creation and deletion of files and folders, you may need to add extra logic to your code to distinguish between the two situations.
This article about Java judging whether a file or folder exists, does not exist, and does not exist, and deletes exist. This is the end of this article. For more related Java judging whether a file or folder exists, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!