简体   繁体   English

fs.writeFile()仅保存部分字符串

[英]fs.writeFile() only saving part of string

I'm creating a text editor using node-webkit. 我正在使用node-webkit创建一个文本编辑器。 When the user clicks a "Save" menu item, I get write a plain text file to disk using the fs.writeFile() method: 当用户单击“保存”菜单项时,我将使用fs.writeFile()方法将纯文本文件写入磁盘:

fs.writeFile(file, txt, function (err) {
  if (err) throw err;
  console.log("file saved");
});

However, it's not saving the entire string passed through the "txt" variable. 但是,它不会保存通过“ txt”变量传递的整个字符串。 It's only saving the first 300 characters or so to the file. 它只会将前300个字符左右保存到文件中。

I've tried using this method, and the synchronous method fs.writeFileSync. 我尝试使用此方法和同步方法fs.writeFileSync。 Both are having the same problem. 两者都有相同的问题。 I've tried logging the txt string passed to the method to make sure there's nothing wrong there. 我尝试记录传递给该方法的txt字符串,以确保那里没有问题。

Any ideas why I'm not getting the full text in my saved file? 为什么我在保存的文件中没有得到全文的任何想法?

According to this post: https://groups.google.com/forum/#!topic/node-webkit/3M-0v92o9Zs in the node-webkit Google group, it is likely an encoding issue. 根据这篇文章:node-webkit Google群组中的https://groups.google.com/forum/#!topic/node-webkit/3M-0v92o9Zs ,很可能是编码问题。 Try changing the encoding. 尝试更改编码。 I was having the same problem and changed my encoding to utf16le, as specified in that thread, and it fixed the issue; 我遇到了同样的问题,并将该线程中指定的编码更改为utf16le,它解决了该问题; the whole string was written to the file. 整个字符串已写入文件。

My code is now: fs.writeFileSync(path, data, {encoding:'utf16le'}); 我的代码现在是: fs.writeFileSync(path, data, {encoding:'utf16le'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM