SoFunction
Updated on 2025-04-11

Solve the pitfalls encountered

() The pitfall encountered

Overview

Get the file object file through new File(filePath), and determine whether the incoming path is a folder through (), but this method can only be adapted to the situation where the folder already exists

Detailed explanation

When the folder already exists, use the isDirectory method to determine whether the incoming path is a folder

String path = "D:\repo\JavaDemo\FileTransfer\static\service\file1";

File file = new File(str);

(());  // true

However, when the path you pass in does not exist in the disk directory, it cannot be judged and will always return false

String path = "D:\repo\JavaDemo\FileTransfer\static\service\zhangsan";

File file = new File(str);

(());  // false

Solution

You can use regular expressions to judge, but the end of the path must be added \

String path = "D:\repo\JavaDemo\FileTransfer\static\service\zhangsan\\";

if(("^[A-z]:\\\\(.+?\\\\)*$")){
   ("Folder!!!");
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.