[英]Javascript: Replace Last Line In .txt File
I wish to open a file and replace the last line. 我希望打开一个文件并替换最后一行。 Not the empty last line, the last last with text, forexample; 不是空的最后一行,例如最后一个带有文本的行;
Line1
Line2
Line3
I would like to transform this into 我想把它变成
Line1
Line2
Last Line
With Windows Scripting Host: 使用Windows脚本宿主:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("test.txt");
var lines = fh.ReadAll();
fh.Close();
lines = lines.split('\r\n');
lines = lines.slice(0, -2);
lines.push('Last Line');
lines = lines.join('\r\n');
var fh = fso.CreateTextFile("test.txt", true);
fh.Write(lines);
fh.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.