JS:对txt文件进行读写,替换和添加

亲测必须将代码放在记事本中,改成html格式,在用IE运行(必须)

<!DOCTYPE html>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		<title></title>
	</head>
	<script language="JavaScript">
		function TextStreamTest(fname,Addname,n){
			var fso,f,ts,s;
			var ForRWA=0,ForReading=1,ForWriting=2,ForAppending=8;
			var TristateUseDefault=-2,TristateTrue=-1,TristateFalse=0;
			fso=new ActiveXObject("Scripting.FileSystemObject");
			var s1=Addname.innerHTML;
			if(fname.value!=""){
				f=fso.GetFile(fname.value);
				switch(n){
					case 1:ForRWA=ForWriting;break;
					case 2:ForRWA=ForAppending;break;
				}
				if(n>0){
					ts=f.OpenAsTextStream(ForRWA,TristateUseDefault);
					var s1=Addname.innerHTML;
					ts.Write(s1);
					s=ts.ReadLine();
					ts.Close();
				}
				ts=f.OpenAsTextStream(ForReading,TristateUseDefault);
				s=ts.ReadLine();
				ts.Close();
			}
			return(s);
		}
		function run(n){
			document.form3.textarea1.innerHTML=TextStreamTest(document.form5.text1,document.form4.textarea2,n);
		}
	</script>
	<body>
		<table width="392" border="1">
			<tr>
				<td height="24" colspan="2">
					<form name="form5" method="post" action="">
						文件路径:<input name="text1" type="textfield" value="E:\ss.txt" size="40">
					</form>
				</td>
			</tr>
			<tr>
				<td width="207" height="163">
					<form name="form3" method="post" action="">
						<textarea name="textarea1" cols="30" rows="10"></textarea>
					</form>
				</td>
				<td>
					<form name="form4" method="post" action="">
						<textarea name="textarea2" rows="10"></textarea>
					</form>
				</td>
			</tr>
			<tr>
				<td height="28">
					<form name="form1" method="post" action="">
						<input type="button" name="Button" value="读取" onclick="run(0)">
					</form>
				</td>
				<td>
					<form name="form2" method="post" action="">
						<input type="button" name="Submit2" value="写入" onclick="run(1)">
						<input type="button" name="Button" value="添加" onclick="run(2)">
					</form>
				</td>
			</tr>
		</table>
	</body>

</html>

运行:

写入:即替换

添加:即在后面追加

猜你喜欢

转载自blog.csdn.net/qq_42192693/article/details/82313934