将字节数组保存到文件节点JS [英] Save byte array to file node JS

查看:827
本文介绍了将字节数组保存到文件节点JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存bytearray到节点js中的文件,对于android我正在使用下面的代码示例。可以任何一次建议我类似的方法?

 文件文件=新文件(根,System.currentTimeMillis()+.jpg ); 
if(file.exists())
file.delete();
FileOutputStream fos = null;
尝试{
fos = new FileOutputStream(file);
fos.write(bytesarray);
fos.close();
返回文件;
}
catch(FileNotFoundException e){
e.printStackTrace();

catch(IOException e){
e.printStackTrace();


解决方案

使用 fs.writeFile 将字符串或字节数组写入文件。




  • 档案< String> | <缓冲液> | <整数> 文件名或文件描述符
  • 数据< String> | <缓冲液> | < Uint8Array>

  • 选项< Object> | < String>


    • 编码< String> | < Null> default ='utf8'

    • mode < Integer> default = 0o666

    • 标记< String> default ='w'

    • 回调 < / code>
      < / li>
      < b>
      $ b<一个文件,如果它已经存在,替换文件。数据可以是字符串或缓冲区。



      如果数据是缓冲区,编码选项将被忽略。默认为'utf8'。



      $ p $ fs.writeFile('message.txt','Hello Node.js',(err )=> {
      if(err)throw err;
      console.log('It'\\'s saved!');
      });


      I want to save bytearray to a file in node js, for android I'm using the below code sample. Can any once suggest me the similar approach?

      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();
      }
      

      解决方案

      Use fs.writeFile to write string or byte array into a file.

      • file <String> | <Buffer> | <Integer> filename or file descriptor
      • data <String> | <Buffer> | <Uint8Array>
      • options <Object> | <String>
        • encoding <String> | <Null> default = 'utf8'
        • mode <Integer> default = 0o666
        • flag <String> default = 'w'
        • callback <Function>

      Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.

      The encoding option is ignored if data is a buffer. It defaults to 'utf8'.

      fs.writeFile('message.txt', 'Hello Node.js', (err) => {
        if (err) throw err;
        console.log('It\'s saved!');
      });
      

      这篇关于将字节数组保存到文件节点JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆