开发者

Check if file path is correct without checking if file exists

开发者 https://www.devze.com 2023-03-06 13:06 出处:网络
I am working on an application installer. The user has to provide a file / d开发者_JAVA百科irectory path that will later be created by the application. I need to check if that path is valid (could exi

I am working on an application installer. The user has to provide a file / d开发者_JAVA百科irectory path that will later be created by the application. I need to check if that path is valid (could exists). The problem is that I can not call file.exists() because it might not exist.

I know I could try to create it and then delete it. If it fails, the path is not valid. But is there a better, simpler way? Also, my work around requires special permissions for my installer which is not very good.


Depends on your criteria for a valid path.

  • If you mean: path is well formed: Use regular expressions
  • If you mean: path is available, try !file.exists();

A regular expression for Unix based systems could be like this: [^\0]+

Also,you'll find lots more information in this SO Question

0

精彩评论

暂无评论...
验证码 换一张
取 消