その具体例を作成しました。
以下ソース
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<title>fso</title> | |
<body style="text-align:center;overflow:hidden"> | |
<textarea id=ta wrap=off style="width:100%;height:100px"></textarea> | |
<button id=btnW>Write</button> | |
<button id=btnR>Read</button> | |
<button id=btnC>Clear</button> | |
<button id=btnN>notepad</button> | |
<label><input type=checkbox id=chk>Unicode</label><br> | |
<select id=sel></select><input id=inp style="width:100px;text-align:center"> | |
</body> | |
<script> | |
resizeTo(250,250) | |
fso=function(){ | |
var fs=new ActiveXObject('Scripting.FileSystemObject') | |
return { | |
AXO:fs, | |
gsf2:fs.GetSpecialFolder(2), // Z:\TEMP | |
Read:function(path, swCreate, swUnicode){ | |
if(!fs.FileExists(path)){return} | |
with(fs.OpenTextFile(path, 1, swCreate, swUnicode)){ var str=AtEndOfStream ? '' : ReadAll(); Close() } | |
return str | |
}, | |
Write:function(path,str,swUnicode,sw上書きしない){ with(fs.CreateTextFile(path, !sw上書きしない, swUnicode)){ Write(str); Close() } } | |
} | |
}() | |
path = fso.gsf2+'/'+(new Date()).getTime() | |
onbeforeunload = function(){ fso.AXO.FileExists(path) && fso.AXO.DeleteFile(path) } | |
btnW.onclick=function(){ fso.Write(path, ta.value, chk.checked) } | |
btnR.onclick=function(){ ta.value = fso.Read(path, false, chk.checked) } | |
btnC.onclick=function(){ ta.value = '' } | |
btnN.onclick=function(){ (new ActiveXObject('WScript.Shell')).Run('notepad '+path) } | |
ta.onchange=function(){ | |
var ops=sel.options, arr=this.value.split('') | |
while(ops[0]){ops[0] = null} | |
for(var i=0,L=arr.length;i<L;i++){ ops[i] = new Option(arr[i]) } | |
sel.onchange() | |
} | |
sel.onchange = function(){ | |
var si=this.selectedIndex | |
inp.value = si<0 ? '' : this.options[si].text.charCodeAt(0) | |
} | |
ta.value = String.fromCharCode(771) | |
ta.onchange() | |
</script> | |
</html> |
起動画面

「Unicode」のチェックを外した状態で、charCode=771の文字を含むテキストをWriteしようとするとエラーになります。

「Unicode」のチェックを入れてWriteした場合はエラーになりません。

「notepad」を押すとメモ帳が起動します。
保存用ウィンドウを出してみると、「文字コード」が「Unicode」になっています。

Writeは「Unicode」のチェックの有無でエラーが出たり、出なかったりするのですがReadの方はUnicodeファイルを開く時にチェックが入っていなくても、ASCIIファイルを開く時にチェックが入っていてもエラーにはなりません。
ただし文字化けします。

↑でUnicodeとしてWriteして、↓でASCIIとしてReadした

↓UnicodeとしてReadした結果。

0 件のコメント:
コメントを投稿