node.js 在 fs.writeFile([option]) 中,“选项参数”通常如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27920892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In fs.writeFile([option]), how an "options parameter" generally work?
提问by iamwave007
I was reading this document about Node.js file system, fs.writeFile(filename, data, [options], callback).
So I noticed that i have seen the [options] pretty often, but never used it for anything. Can someone give me an example? All the cases i had didn't use this option.
我正在阅读有关 Node.js 文件系统的文档,fs.writeFile(filename, data, [options], callback). 所以我注意到我经常看到 [options],但从来没有用过它。有人可以举个例子吗?我遇到的所有情况都没有使用此选项。
回答by uber5001
I'm guessing your interested in how an optionsparameter generally works in javascript.
我猜您对options参数在 javascript 中的一般工作方式感兴趣。
As opposed to whatthe parameters are, which are stated in the docs:
而不是什么参数是,这是在文档中阐明:
- optionsObject
- encodingString| Null default = 'utf8'
- modeNumberdefault = 438 (aka 0666 in Octal)
- flagStringdefault = 'w'
- 选项对象
- 编码字符串| 空默认 = 'utf8'
- 模式编号默认值 = 438(八进制中又名 0666)
- 标志字符串默认 = 'w'
Generally, the optionsparameter is an object, with properties that are the options you want to modify. So if you wanted to modify two of the options on fs.writeFile, you'd add each one as a property to options:
通常,options参数是一个对象,其属性是您要修改的选项。因此,如果您想修改 上的两个选项fs.writeFile,您可以将每个选项作为属性添加到options:
fs.writeFile(
"foo.txt",
"bar",
{
encoding: "base64",
flag: "a"
},
function(){ console.log("done!") }
)
And if you're confused as to what these three params are used for, the docs for fs.openhave everything you need. It includes all the possibilities for flag, and a description for mode. The callbackis called once the writeFileoperation is complete.
如果您对这三个参数的用途感到困惑,那么 docs forfs.open有您需要的一切。它包括 的所有可能性flag,以及 的描述mode。在callback被调用一次的writeFile操作完成。
回答by Artif3x
For anyone ending up here off a search looking for a flags reference, here it is:
对于最终在这里寻找标志参考的任何人,这里是:
Flag Description
r Open file for reading. An exception occurs if the file does not exist.
r+ Open file for reading and writing. An exception occurs if the file does not exist.
rs Open file for reading in synchronous mode.
rs+ Open file for reading and writing, asking the OS to open it synchronously. See notes for 'rs' about using this with caution.
w Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
wx Like 'w' but fails if the path exists.
w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
wx+ Like 'w+' but fails if path exists.
a Open file for appending. The file is created if it does not exist.
ax Like 'a' but fails if the path exists.
a+ Open file for reading and appending. The file is created if it does not exist.
ax+ Like 'a+' but fails if the the path exists.
Flag Description
r Open file for reading. An exception occurs if the file does not exist.
r+ Open file for reading and writing. An exception occurs if the file does not exist.
rs Open file for reading in synchronous mode.
rs+ Open file for reading and writing, asking the OS to open it synchronously. See notes for 'rs' about using this with caution.
w Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
wx Like 'w' but fails if the path exists.
w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
wx+ Like 'w+' but fails if path exists.
a Open file for appending. The file is created if it does not exist.
ax Like 'a' but fails if the path exists.
a+ Open file for reading and appending. The file is created if it does not exist.
ax+ Like 'a+' but fails if the the path exists.
回答by Laksh Goel
fs.writeFile(filename,data,{flag: "wx"},function(err){
if(err) throw err
console.log('Date written to file, ',filename)
})
As you can see in the above code snippet, the third parameter is the options/flag. There are optional and used to indicate the behaviour of the file to be opened.
正如您在上面的代码片段中看到的,第三个参数是选项/标志。有可选的,用于指示要打开的文件的行为。
I have passed "wx" as option which indicates, file will open for writing and will be created if it doesn't exist. But it will fail if already exists.
我已经传递了“wx”作为选项,它表示文件将打开进行写入,如果它不存在则将被创建。但如果已经存在,它将失败。
By default "w" is passed as option.
默认情况下,“w”作为选项传递。
For further reading on different options, here
如需进一步阅读不同选项,请点击此处
回答by Sandeep Panwar
These are the options.
这些是选项。
- encoding (string or NULL), default value is 'utf8'
- mode (number), default value is 438 (aka 0666 in Octal)
- flag (string), default value is 'w'
- 编码(字符串或NULL),默认值为'utf8'
- 模式(数字),默认值为 438(八进制中又名 0666)
- 标志(字符串),默认值为 'w'

