Skip to content

Instantly share code, notes, and snippets.

@kymtwyf
Created November 24, 2015 12:44
Show Gist options
  • Save kymtwyf/4fe6dbbc297fb1f22a0c to your computer and use it in GitHub Desktop.
Save kymtwyf/4fe6dbbc297fb1f22a0c to your computer and use it in GitHub Desktop.
Intellij Error(1,1)illegalcharacter '\ufeff'

参考

var fs = require('fs');
var path = "目标路径..";


function readDirectory(dirPath) {
    if (fs.existsSync(dirPath)) {
        var files = fs.readdirSync(dirPath);
        
        files.forEach(function(file) {
            var filePath = dirPath + "/" + file;
            var stats = fs.statSync(filePath);

            if (stats.isDirectory()) {
                console.log('\n读取目录:\n', filePath, "\n");
                readDirectory(filePath);
            } else if (stats.isFile()) {
                var buff = fs.readFileSync(filePath);
                if (buff[0].toString(16).toLowerCase() == "ef" && buff[1].toString(16).toLowerCase() == "bb" && buff[2].toString(16).toLowerCase() == "bf") {
                    //EF BB BF 239 187 191
                    console.log('\发现BOM文件:', filePath, "\n");

                    buff = buff.slice(3);
                    fs.writeFile(filePath, buff.toString(), "utf8");
                }
            }
        });        

    } else {
        console.log('Not Found Path : ', dirPath);
    }
}

readDirectory(path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment