search

Home  >  Q&A  >  body text

javascript - Can stat() access() open() readFile() in node.js determine whether a file exists?

My understanding is:
stat() is to obtain file information;
access() tests whether the file is available;
open() opens the file;
readFile() reads the file;
Then I can determine whether the file is available when using readFile(), but I see that some people write static resource servers first using the open() method. Isn’t this redundant? Are there any other considerations here? ?

某草草某草草2716 days ago828

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-06-24 09:45:34

    If you simply determine whether the file exists, it is recommended to use the fs.existsSync() method directly, refer to the official API

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-24 09:45:34

    Stat is to obtain file information and is usually not used to determine whether the file is available.

    Access is best suited to determine whether a file is available.

    There is a big difference between open and readFile. Open can read and write. It can also create a file or directly fail based on whether the file exists.

    reply
    0
  • Cancelreply