Java – is there any reason why some methods in file use Boolean values to indicate its success (not just throwing exceptions)?

The file class in java contains methods that use Boolean values to indicate the success of the operation being performed The user of the method needs to check the return value every time it is called

The following is a summary of the document obtained from MKDIR (), which describes the requirements:

There is also a case of createnewfile() (even more strange) using Boolean values and throwing exceptions to indicate success:

Now, this seems inconvenient because the user has to predict two error conditions instead of just using a simple try catch block

What is the reason for this fuss?

Solution

Because this is the way they designed it, more than 20 years ago If you can let developers leave their nursing home and leave the Zimmer framework, you may get a better answer Otherwise we're all just guessing

However, you don't need to call these methods often, because some people here seem to be thinking For example, isfile () / exists () / delete () / createnewfile () is redundant before the new FileInputStream (...) or the new fileoutputstream (...), which will completely throw the exception you want to find Call File. before any of them or corresponding FileReader / Writer constructors. Exists () / delete () / createnewfile () is worse than useless. This is a positive waste of time and space. The work of constructor (or, more accurately, the system code called by operating system code) must be repeated I suspect I've used file for 20 years createNewFile().

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>