日期:2014-05-17  浏览次数:20768 次

如何读取文本文件并进行字段分割?
我有一个文本文件,要用asp读取并对其进行一些处理。
把用户名和IP地址和时间等记录分割出来保存到数据库里。
我用FSO来读取了文件,请问我应该如何对读取到的内容进行分割呢?
哪位兄台指点一点。最好能写出实现过程的代码,谢谢

'----读取文本文件
Set   FSO   =   Server.CreateObject( "scripting.filesystemobject ")
Set   Text_Stream   =   FSO.OpenTextFile(Server.MapPath( "line.txt "),1,true)
i   =   0
Do   Until   Text_Stream.AtEndOfStream
        s   =   Text_Stream.ReadLine
        i   =   i   +   1
Loop


line.txt文件内容:
Test   CLIENT   LIST
Updated,Wed   May   30   13:00:02   2007
Common   Name,Real   Address,Bytes   Received,Bytes   Sent,Connected   Since
abc001,211.94.164.212:34836,49478,175209,Wed   May   30   12:50:03   2007
abc002,211.94.164.212:12644,587470,6735429,Wed   May   30   12:11:46   2007
abc005,211.94.164.218:2059,4708,45209,Wed   May   30   12:59:37   2007
ROUTING   TABLE

------解决方案--------------------
自己设定分割符~
程序读到分割符自动识别 并把已识别的放到一个数组里
------解决方案--------------------
直接在你的代码上修改一下:
Set FSO = Server.CreateObject( "scripting.filesystemobject ")
Set Text_Stream = FSO.OpenTextFile(Server.MapPath( "line.txt "),1,true)
i = 0
Do Until Text_Stream.AtEndOfStream
s = Text_Stream.ReadLine
i = i + 1
if i > = 4 then
arr_s = split(s , ", ")
if ubound(arr_s) <> 5 then
response.write( "line " & i & " have error ")
response.end()
end if

username = arr_s(0)
userIP = arr_s(1)
... /*逐个得到需要的数据,然后写入数据库就可以,希望对你有帮助
end if
Loop


------解决方案--------------------
用SPLIT函数就OK了