Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodeJS中的给定编码错误_Node.js - Fatal编程技术网

Node.js nodeJS中的给定编码错误

Node.js nodeJS中的给定编码错误,node.js,Node.js,我只是在NodeJS上尝试一些代码,我是NodeJS的新手。我已经编写了以下代码块 var fs = require('fs'), os = require('os'); var filename = 'Server.ini'; var serverData = os.hostname() + "\n" + os.platform() + "\n" + os.type() + "\n"; fs.existsSync(filename, function(exists) { i

我只是在NodeJS上尝试一些代码,我是NodeJS的新手。我已经编写了以下代码块

var fs = require('fs'),
    os = require('os');

var filename = 'Server.ini';
var serverData = os.hostname() + "\n" + os.platform() + "\n" + os.type() + "\n";

fs.existsSync(filename, function(exists) {
    if(exists) {
        console.log("1. " + filename + " file found. Server needs to be updated.")

        fs.unlinkSync(filename, function(error) {
            if(error) throw error;
            console.log("2. " + filename + " is been unlinked from server.");
        });

    } else {
        console.log("1. " + filename + " not found.");
        console.log("2. Server needs to be configured.");
    }
});

fs.openSync(filename, "w+", function(error) {
    if(error) throw error;
    console.log("3. " + filename + " file is been locked.");
}); 

fs.writeFileSync(filename, serverData, function(error) {
    if(error) throw error;
    console.log("4. " + filename + " is now updated.");

    fs.readFileSync(filename, 'utf-8', function(error, data) {
        if(error) throw error;

        console.log("5. Reading " + filename + " file");
        console.log("6. " + filename + " contents are below\n");
        console.log(data);
        console.log("-------THE END OF FILE-------");
    });
});
我已经编辑了代码并添加了同步,但现在它给了我以下错误:

D:\NodeJS\fs>node eg5.js

buffer.js:382
      throw new Error('Unknown encoding');
            ^
Error: Unknown encoding
    at Buffer.write (buffer.js:382:13)
    at new Buffer (buffer.js:261:26)
    at Object.fs.writeFileSync (fs.js:758:12)
    at Object.<anonymous> (D:\NodeJS\fs\eg5.js:28:4)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

D:\NodeJS\fs>
D:\NodeJS\fs>node eg5.js
buffer.js:382
抛出新错误(“未知编码”);
^
错误:未知编码
在Buffer.write(Buffer.js:382:13)
在新的缓冲区(Buffer.js:261:26)
在Object.fs.writeFileSync(fs.js:758:12)
反对。(D:\NodeJS\fs\eg5.js:28:4)
在模块处编译(Module.js:449:26)
在Object.Module._extensions..js(Module.js:467:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Module.runMain(Module.js:492:10)
在process.startup.processNextTick.process.\u tickCallback(node.js:244:9)
D:\NodeJS\fs>

我的代码中有关于utf8的错误吗

readFileSync
不接受回调。我猜你想用
readFile
来代替

对于
writeFileSync
,您也有同样的问题。当同步使用IO函数时,完成时调用的回调没有意义。最好使用异步函数(不使用“Sync”),注意它们采用不同的参数


文档中总是提到“utf8”,而不是“utf-8”,我不知道是否支持后者。

根据node.js API文档,writeFileSync有3个参数:

  • 要写入的文件名
  • 要放入文件的数据
  • 包含选项的可选对象,其中一个选项是编码
  • 它没有指定回调。只有异步版本接受回调

    请尝试以下操作,而不是writeFileSync块:

    fs.writeFileSync(filename, serverData, { encoding: 'utf8'});
    console.log("4. " + filename + " is now updated.");
    
    var contents = fs.readFileSync(filename, 'utf8');
    console.log("5. Reading " + filename + " file");
    console.log("6. " + filename + " contents are below\n");
    console.log(contents);
    console.log("-------THE END OF FILE-------");
    

    更重要的是,writeFileSync不接受回调。但是,它确实需要编码。是的,“utf-8”看起来是受支持的: