如何将ListView中的内容保存成文件并可以读出。(50分)

  • 主题发起人 commandor
  • 开始时间
C

commandor

Unregistered / Unconfirmed
GUEST, unregistred user!
有一Listview,我通过拖拽文件进去。现在是要将ListView中的内容保存成自定义格式的文件
,并要能用listView读出此文件。

问:如何自定义文件格式(其实就是文本文件),如何保存并读出该文件。

最好有实际例子。
 
// save
对于listview,你可以通过caption, subitems读出一行的各个位置的文字
再通过listview.items.count可以读出每一行的文字
通过一定的分隔符号,如:TAB来区别listview中的caption, subitems
将listview中的每一行读进一个stringlist ,通过stringlist.savetofile保存
// load
再通过stringlist.loadfromfile读出之前的文件
对于stringlist的每一行,通过AnsiPos, Copy可以得到相应的Listview的信息

就是这样的简单啦,明白了吗?
 
你可以用INI文件来保存
比如 //用readinteger来循环读取
[listview]
count=12; //保存一共有多少个Listitem

[1] //第一个结点
name=123 //item的标题
mail=1234@fafa.com //子结点的数据
 
这是我经常使用的一个例子
procedure ListViewToTxt;
var
i : integer;
TFile : string;
Text : TStringList;
begin
Text:=TStringList.Create;
try
for i:=0 to TraceLists.Items.Count-1 do
Text.Add(Format(ListView1.Items.Caption,
ListView1.Items.SubItems[0],ListView1.Items.SubItems[1],
ListView1.SubItems[2]]));
TFile:=GetCurrentDir+'/'File1.txt';
i:=0;
while FileExists(TFile) do
begin
TFile:=GetCurrentDir+'/File'+IntToStr(i)+'.txt';
inc(i);
end;
Text.SaveToFile(TFile);
finally
TraceText.Free;
end;
end;

 
最后应该是:)
finally
Text.Free;
end;
end;
 
谢谢楼上的各位帮助:)

那从file再返回到ListView中呢?
 
多人接受答案了。
 
顶部