이전 동료는 도구를 썼지 만 버그가있었습니다. 즉, 파일을 교체 한 후 원본 파일의 형식은 UTF8 BOM이되었습니다. BOM이있는 이런 종류의 XML은 Mac에서 읽지 않을 수 있으므로 처리 할 도구를 작성해야합니다.
사실, 아이디어는 비교적 간단합니다. 먼저 디렉토리를 반복 한 다음 디렉토리를 읽고 파일의 첫 세 바이트를 제거한 다음 파일을 UTF-8 형식으로 저장하십시오. 코드를 입력하십시오 :)
코드 사본은 다음과 같습니다.
var fs = 요구 ( 'fs');
var path = "대상 경로 ...";
함수 readDirectory (dirpath) {
if (fs.existsSync (dirpath)) {
var files = fs.readDirsync (dirpath);
files.foreach (function (file) {
var filepath = dirpath + "/" + 파일;
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 ( '/Discover Bom 파일 :', filepath, "/n");
buff = buff.slice (3);
fs.writefile (filepath, buff.tostring (), "utf8");
}
}
});
} 또 다른 {
console.log ( '찾을 수 없음 :', dirpath);
}
}
readDirectory (경로);