gpt4 book ai didi

javascript - 使用 OS.File 在文件中持久存储 JSON

转载 作者:行者123 更新时间:2023-11-29 19:30:39 25 4
gpt4 key购买 nike

如何以独立于操作系统的方式使用 OS.File 在文件中的 JSON 的 firefox 插件 sdk 中进行持久性存储?就像我将文件保存在 Windows 中的 D:\file 中一样,它在 linux 中甚至在没有驱动器的 Windows 中都无法工作 :D。

我该怎么做?

最佳答案

realObject 作为 JSON 存储在文件 MyFileName.json 中,该文件是在目录中的 extension-data 目录中创建/覆盖的当前的配置文件,你可以这样做:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");

let asJSON = JSON.stringify(realObject,null);
let file2 = openFileInPrefsExtensionData("MyFileName.json");
overwriteTextFileFromString (file2, asJSON);


/**
* Open file in Extension's extension-data directory under the pref Directory.
* This is the correct place to store files for your extension under the profile's
* directory.
*/
function openFileInPrefsExtensionData(filename) {
let fileNameList = ["extension-data","MyExtensionID"];
fileNameList.push(filename);
//This does create all directories along the way to the file,
// but the file itself is not created.
return FileUtils.getFile("ProfD", fileNameList);
}


/**
* Overwrite a file from a string.
* Currently this just overwrites, without any callback function at the end
* of the write. If the write fails, then it is reported in the console,
* but not otherwise handled.
*/
function overwriteTextFileFromString(file,data) {
overwriteTextFile(file, data, function (status) {
});
}

/**
* Overwrite a file with a string.
*/
function overwriteTextFile(nsiFile, data, flags, callback) {
//data is data you want to write to file
//if file doesnt exist it is created
// You can also optionally pass a flags parameter here. It defaults to
// FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;

//PR_RDONLY 0x01 Open for reading only.
//PR_WRONLY 0x02 Open for writing only.
//PR_RDWR 0x04 Open for reading and writing.
//PR_CREATE_FILE 0x08 If the file does not exist, the file is created. If the file exists, this flag has no effect.
//PR_APPEND 0x10 The file pointer is set to the end of the file prior to each write.
//PR_TRUNCATE 0x20 If the file exists, its length is truncated to 0.
//PR_SYNC 0x40 If set, each write will wait for both the file data and file status to be physically updated.
//PR_EXCL 0x80 With PR_CREATE_FILE, if the file does not exist, the file is created. If the file already exists, no action and NULL is returned.

let args = Array.prototype.slice.call(arguments,4);

if(typeof flags == "undefined") {
//These are already the defaults.
flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
}
//XXX NOTE: Flags is currently not being used. I don't recall why I disabled its use.

var ostream = FileUtils.openSafeFileOutputStream(nsiFile);
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
var istream = converter.convertToInputStream(data);
// The last argument (the callback) is optional.
//asyncCopy automatically closes both the input and output streams upon completion.
NetUtil.asyncCopy(istream, ostream, function (status) {
if (!Components.isSuccessCode(status)) {
// Handle error!
Components.utils.reportError('error on write isSuccessCode = ' + status);
return;
}
// Data has been written to the file.
//send the status, and any other args to the callback function.
args.unshift(status);
if(typeof callback == "function" ) {
//there is a callback function..
callback.apply(callback, args);
}
});
}

关于javascript - 使用 OS.File 在文件中持久存储 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com