(转载)JS读写txt文件
参考 spring 用户的文章,多谢!!地址(http://blog.163.com/huangjian_uu/blog/static/12891973920102224395146/)
一同学找我帮忙说js读写文件的时候遇到问题了,于是查了大半天时间的资料终于找到一篇能解决问题的文章,不容易啊!!自己也受益匪浅!!(其实拿到代码后一步一个弹框之后就知道问什么要这么写了)
下面是复制原版的:
最初的程序如下:
//读文件
function readFile(filename){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n";
f.Close();
return s;
}
//写文件
function writeFile(filename,filecontent){
var fso, f, s ;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile(filename,8,true);
f.WriteLine(filecontent);
f.Close();
alert('ok');
}
这两个函数在参数filename为绝对路径时是没问题的,但是若为相对路径则无法正确运行 在网上查了不少资料,有人说OpenTextFile()的第一个参数既可以是绝对路径也可以是相对路径,也有人说只能是绝对路径
无论孰是孰非,我确实是用了相对路径时就无法正确运行。但是程序的移植性又要求参数不能直接传绝对路径~
后来想到document.location.pathname包含当前路径信息,不如用它做相应处理以获得当前路径,再和需要打开的文件连在一块不就成了绝对路径了?捏哈哈~~
于是有了以下代码:
//获取绝对地址
function abPath(){
var absolutePath = '';
var pathArray = new Array();
var currentFileLocation = document.location.pathname; //alert(currentFileLocation);
pathArray = currentFileLocation.split("\\");
pathArray[0] = pathArray[0].substr(1,pathArray[0].length-1);
/***********************************************************/
/*做这样的处理是因为alert(currentFileLocation)弹出:*/
/* /D:\alpha\test.html */
/* 当然test.html是调用这些函数的当前页面啦 */
/***********************************************************/
for(var i = 0; i<pathArray.length; i++){
if(pathArray[i] != 'test.html')
absolutePath = absolutePath + pathArray[i] + "\\";
}//alert(absolutePath);
return absolutePath;
}
//读文件
function readFile(filename){
var absolutePath = abPath();
filename = absolutePath + filename; //alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n";
f.Close();
s = parseInt(s);
return s;
}
//写文件
function writeFile(filename,filecontent){
var absolutePath = abPath();
filename = absolutePath + filename; //alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,2,true);
f.WriteLine(filecontent);
f.Close();
}
这下好了,调用的时候可以传相对路径了~~~~
我用来测试的浏览器是IE6,后来发现在IE7、IE8中却又无法正确运行了 用alert(currentFileLocation)一测试,发现它们弹出的路径信息为:/D:/alpha/test.html,与IE6的斜杠方向不同,所以要做不同的处理,同时还要增加浏览器版本判断,代码如下:
//判断是否ie6
function isIe6(){
var browserName = navigator.appName; //alert(browserName);
var browserVer = navigator.userAgent.toLowerCase();
if(browserName == "Microsoft Internet Explorer"){
if(browserVer.match(/msie ([\d.]+)/)[1] < 7) return true;
}
else return false;
}
//获取绝对地址
function abPath(){
var absolutePath = '';
var pathArray = new Array();
var loopindex = 0;
var currentFileLocation = document.location.pathname; alert(currentFileLocation);
if(isIe6()){
pathArray = currentFileLocation.split("\\");
pathArray[0] = pathArray[0].substr(1,pathArray[0].length-1);
} else {
pathArray = currentFileLocation.split("/");
}
if(pathArray[0] == '')loopindex = 1;
for(loopindex; loopindex<pathArray.length; loopindex++){
if(pathArray[loopindex] != 'test.html')
absolutePath = absolutePath + pathArray[loopindex] + "\\";
}//alert(absolutePath);
return absolutePath;
}
//读文件
function readFile(filename){
var absolutePath = abPath();
filename = absolutePath + filename; //alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var s = "";
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n";
f.Close();
s = parseInt(s);
return s;
}
//写文件
function writeFile(filename,filecontent){
var absolutePath = abPath();
filename = absolutePath + filename; //alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,2,true);
f.WriteLine(filecontent);
f.Close();
}
到此IE浏览器都可以正确读写文件了,有个缺憾就是在firefox浏览器中不能正确运行,因为firefox浏览器貌似不支持ActiveXObject控件,因而var fso = new ActiveXObject("Scripting.FileSystemObject"); 也就没什么用了~ 唉
在传相对路径参数时,要用右斜杠,还要转义,也就是要用两个右斜杠分隔……用左斜杠或者单斜杠都不行,我不知道是我比较特殊还是大家都这样
下面再附一段最简练的代码(说是简练,其实是没有考虑其他的一些bug的问题,只是完成了最基本的读写操作。)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script language="javascript">
//获取绝对地址
function abPath(){
var absolutePath = '';
var pathArray = new Array();
var loopindex = 0;
var currentFileLocation = document.location.pathname; alert(currentFileLocation);
pathArray = currentFileLocation.split("/");//alert(pathArray);//,D:,Cache,优酷,99.html
if(pathArray[0] == '')loopindex = 1;
for(loopindex; loopindex<pathArray.length; loopindex++){
if(pathArray[loopindex] != '99.html')
absolutePath = absolutePath + pathArray[loopindex] + "\\";
//alert(absolutePath);
}//alert(absolutePath);
return absolutePath;
}
//读文件
function readFile(filename){
var absolutePath = abPath();
filename = absolutePath + filename; alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,1);
var ts = "";
while (!f.AtEndOfStream){
ts=f.ReadAll();
alert(ts);
}
f.Close();
}
//写文件
function writeFile(filename){
var absolutePath = abPath();
filename = absolutePath + filename; //alert(filename);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(filename,2,true);
f.WriteLine("测试文字");
f.Close();
}
</script>
<button type="button" onclick="readFile('1.txt')">读</button>
<button type="button" onclick="writeFile('1.txt')">写</button>
</body>
</html>
浙公网安备 33010602011771号