Bap*_*zin 5 javascript file-io synchronous gjs
我正在尝试使用 GJS 更精确地以同步方式读取文本文件。这是一个用于文件读取的异步函数的示例
gio-cat.js 我找到了如何使用下一个函数处理种子:
function readFile(filename) {
print(filename);
var input_file = gio.file_new_for_path(filename);
var fstream = input_file.read();
var dstream = new gio.DataInputStream.c_new(fstream);
var data = dstream.read_until("", 0);
fstream.close();
return data;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它不适用于 GJS。谁能帮我 ?
当我使用 GJS 开发 Cinnamon 小程序时,我曾经使用该get_file_contents_utf8_sync函数来读取文本文件:
const Cinnamon = imports.gi.Cinnamon;
let fileContent = Cinnamon.get_file_contents_utf8_sync("file path");
Run Code Online (Sandbox Code Playgroud)
如果您安装了 Cinnamon 并且同意使用它,它就会回答您的问题。另外,这里是中函数
的 C 代码,希望对您有所帮助:get_file_contents_utf8_synccinnamon-util.c
char * cinnamon_get_file_contents_utf8_sync (const char *path, GError **error)
{
char *contents;
gsize len;
if (!g_file_get_contents (path, &contents, &len, error))
return NULL;
if (!g_utf8_validate (contents, len, NULL))
{
g_free (contents);
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"File %s contains invalid UTF-8",
path);
return NULL;
}
return contents;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1559 次 |
| 最近记录: |