gpt4 book ai didi

node.js - 保存字节数组到文件 Node JS

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:36 25 4
gpt4 key购买 nike

我想将 bytearray 保存到 Node js 中的文件中,对于 Android,我正在使用下面的代码示例,任何人都可以建议我类似的方法

   File file = new File(root, System.currentTimeMillis() + ".jpg");
if (file.exists())
file.delete();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(bytesarray);
fos.close();
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

最佳答案

Leonenko 的答案引用/复制了正确的 JavaScript 文档,但事实证明 writeFile 不能很好地与 Uint8Array 配合使用——它只是将字节写为数字:

“84,104,101,32,102,105,114,115,...”

为了让它工作,必须将 Uint8Array 包装在缓冲区中:

fs.writeFile('testfile',new Buffer(ui8a),...)

关于node.js - 保存字节数组到文件 Node JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41999106/

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