首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么将一个文本文件的内容读取,然后写入到另外一个文本文件

2012-02-04 
如何将一个文本文件的内容读取,然后写入到另外一个文本文件?各位朋友,小弟有一个文本文件,内容和格式大概

如何将一个文本文件的内容读取,然后写入到另外一个文本文件?
各位朋友,小弟有一个文本文件,内容和格式大概如下:

标题
第一行内容
第二行内容
第三行内容
。。。。。
第九百行内容
结尾



现在想将该文本文件的内容分批读出,然后分批存入到另外一个文本文件,第1次先读出第一行到第三百行的内容,然后将这三百行的内容先写入到第一个文本文件,然后继续从301行继续读,将301行到第6百行的内容写入到第二个文本文件,然后从601行的内容开始读,将601行到第900行内容写入到第三个文本文件,请各位朋友给一个思路,应该怎样实现,请各朋友赐教,衷心谢谢!

最后想要的三个文本文件内容如下 :

文件1:
标题
第1行
第2行
。。。
第300行
结尾


文件2:
标题
第301行
第302行
。。。
第600行
结尾


文件3:
标题
第601行
第602行
。。。
第900行
结尾

[解决办法]
逐行读取文本文件,这是以前为另一个网友写的,你可以参考一下

Delphi(Pascal) code
procedure TForm1.Button3Click(Sender: TObject);var  fl:TextFile;  path,s:string;  str:string;  i,j,m,n:Integer;  b:Boolean;begin  path:=ExtractFilePath(Application.ExeName);  AssignFile(fl,path+'text.txt');  Reset(fl);  //逐行读文本文件,************************************************************  repeat    Readln(fl,str);//读取文本文件当前行内容,开始是第一行,然后逐行读取    i:=Pos('XX-ClientMac:',str);//子串在字符串中的位置,大于0说明找到子串,否则无匹配的子串    if i>0 then//有匹配的子串    begin      s:=Copy(str,i+length('XX-ClientMac:'),Length(str)-i-length('XX-ClientMac:')+1);//读取字符串中子串后的内容,就是你要的IP地址    end    else//没有匹配的子串    begin      i:=Pos('XX-ServerMac:',str);//另一子串在字符串中的位置,大于0说明找到子串,否则无匹配的子串      if i>0 then      begin        s:=Copy(str,i+length('XX-ServerMac:'),Length(str)-i-length('XX-ServerMac:')+1);//读取字符串中子串后的内容,就是你要的IP地址      end    end;  until    SeekEof(fl);//文本文件读取完成(英文解释:Returns the end-of-file status of a file, ignoring whitespace.)  CloseFile(fl);  //self.MaskEdit1.EditMaskend;
[解决办法]
写文本文件,这是以前自己程序里的一段代码,可以参考
Delphi(Pascal) code
//将一些报文保存到文本文件中,便于调试时使用***********************************procedure Tw_zjm.writefile_pro(name, content: string);var  fl:TextFile;  k:integer;begin  if not fileexists(name) then  begin    k:=filecreate(name);    if k>0 then    begin      fileclose(k);      assignfile(fl,name);      append(fl);      writeln(fl,datetimetostr(now)+':');      writeln(fl,content);      closefile(fl);    end;  end  else  begin    assignfile(fl,name);    append(fl);    writeln(fl,datetimetostr(now)+':');    writeln(fl,content);    closefile(fl);  end;end;//****************************************************************************
[解决办法]
請自行測試

procedure TForm1.Button3Click(Sender: TObject);
var
Atext,btext:tstringlist;
i:integer;
begin

OpenDialog1.Execute;
if OpenDialog1.FileName<>'' then begin
atext:=Tstringlist.Create;
btext:=Tstringlist.Create;
atext.LoadFromFile(OpenDialog1.FileName);
btext.Add('標題1');
FOr i:=0 to atext.Count-1 do begin
if (i mod 300 =0) and (i>0) then begin
btext.SaveToFile('標題'+floattostr(i/300));
btext.Clear;
btext.Add('標題'+floattostr(i/300+1));
end else
btext.Add(atext[i]);
end;
atext.Free;
btext.Free;
end;

end;
[解决办法]
200W行也就在百秒左右,幾W能花多少時間?你希望有多快啊!

热点排行