javascript一行一行的读取文本
javascript一行一行的读取文本我有个.TXT文本,想用javascript一行一行的读出并alert出来,每间隔10秒读一条,哪个高手能帮忙写个代码啊???...
javascript一行一行的读取文本
我有个.TXT文本,想用javascript一行一行的读出并alert出来,每间隔10秒读一条,哪个高手能帮忙写个代码啊??? 展开
我有个.TXT文本,想用javascript一行一行的读出并alert出来,每间隔10秒读一条,哪个高手能帮忙写个代码啊??? 展开
3个回答
展开全部
<script language="javascript">
var TxtFile = function(ClassName)
{
this.ClassName = ClassName;
this.Fso = null;
this.Delay = 10000;//多长时间读取一次
this.ReadLine = 0;//初始化,从第几行开始读取
this.RowCount = 1;//文本的总行数
this.TxtContent = null;//文本的内容,数组
this.ForReading = 1;//只读
}
TxtFile.prototype.CreateObject = function()
{
if(this.Fso == null)
{
this.Fso = new ActiveXObject("Scripting.FileSystemObject");
}
}
TxtFile.prototype.OpenFile = function(TxtFilePath)
{
this.CreateObject();
try
{
var f = this.Fso.OpenTextFile(TxtFilePath,this.ForReading);
}
catch (e)
{
alert("文件不存在");
return;
}
if(f.AtEndOfLine)
{
alert('空文件!');
this.RowCount = 1;
this.TxtContent = [""];
}
else
{
this.TxtContent = f.ReadAll().split("\r\n");
this.RowCount = this.TxtContent.length;
}
this.ToAlertFileConten();
}
TxtFile.prototype.ToAlertFileConten = function()
{
if(this.ReadLine < this.RowCount )
{
alert(this.TxtContent[this.ReadLine]);
}
if(this.ReadLine ==this.RowCount - 1)
{
alert("文件读取完毕。");
}
else
{
this.ReadLine++;
window.setTimeout(""+ this.ClassName +".ToAlertFileConten();",this.Delay);
}
}
var MyTxtFile = new TxtFile("MyTxtFile");
MyTxtFile.OpenFile("c:\\temp.txt");
</script>
var TxtFile = function(ClassName)
{
this.ClassName = ClassName;
this.Fso = null;
this.Delay = 10000;//多长时间读取一次
this.ReadLine = 0;//初始化,从第几行开始读取
this.RowCount = 1;//文本的总行数
this.TxtContent = null;//文本的内容,数组
this.ForReading = 1;//只读
}
TxtFile.prototype.CreateObject = function()
{
if(this.Fso == null)
{
this.Fso = new ActiveXObject("Scripting.FileSystemObject");
}
}
TxtFile.prototype.OpenFile = function(TxtFilePath)
{
this.CreateObject();
try
{
var f = this.Fso.OpenTextFile(TxtFilePath,this.ForReading);
}
catch (e)
{
alert("文件不存在");
return;
}
if(f.AtEndOfLine)
{
alert('空文件!');
this.RowCount = 1;
this.TxtContent = [""];
}
else
{
this.TxtContent = f.ReadAll().split("\r\n");
this.RowCount = this.TxtContent.length;
}
this.ToAlertFileConten();
}
TxtFile.prototype.ToAlertFileConten = function()
{
if(this.ReadLine < this.RowCount )
{
alert(this.TxtContent[this.ReadLine]);
}
if(this.ReadLine ==this.RowCount - 1)
{
alert("文件读取完毕。");
}
else
{
this.ReadLine++;
window.setTimeout(""+ this.ClassName +".ToAlertFileConten();",this.Delay);
}
}
var MyTxtFile = new TxtFile("MyTxtFile");
MyTxtFile.OpenFile("c:\\temp.txt");
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |