JScript 操作文本文件 练习代码
var console = {
log: function (txt) { WScript.Echo(txt); }
};
var TextStream = function () {
this.handle = null;
this.create = function (filename, overwrite) {
var fso = new ActiveXObject("scripting.filesystemobject");
this.handle = fso.CreateTextFile(filename, overwrite);
}
this.open = function (filename, bWrite, bAppend) {
var fso = new ActiveXObject("scripting.filesystemobject");
var iMode = 1; //ForReading;
if (bWrite == true) {
iMode = 2; //ForWriting;
}
if (bAppend == true) {
iMode = 8; //ForAppending;
}
this.handle = fso.OpenTextFile(filename, iMode, true, -2);
}
this.write = function (data) { return this.handle.write(data); }
this.writeLine = function (data) { return this.handle.WriteLine(data); }
this.writeBlankLine = function (nLine) { if (nLine == null) { nLine = 1; } return this.handle.WriteBlankLines(nLine); }
this.close = function () { return this.handle.Close(); }
this.read = function (nChar) { return this.handle.Read(nChar); }
this.readAll = function (nChar) { return this.handle.ReadAll(); }
this.readLine = function () { return this.handle.ReadLine(); }
this.skip = function (nChar) { return this.handle.Skip(nChar); }
this.skipLine = function () { return this.handle.SkipLine(); }
this.atEndOfLine = function () { return this.handle.AtEndOfLine; }
this.atEndOfStream = function () { return this.handle.AtEndOfStream; }
this.column = function () { return this.handle.Column; }
this.line = function () { return this.handle.Line; }
};
var file = new TextStream();
//file.create("c:\\test.log", true);
file.open("c:\\test.log",true);
file.write("33333");
file.writeBlankLine();
console.log("this line:" + file.line() );
file.writeLine("==================");
//file.skipLine( );
//var str = file.readLine();
//console.log(str);
file.close();
console.log("End............." );