文本文件操作

Qpinguo 2008-08-25 12:21:54
请问:如何在一个文本文件中,删除、插入一行
谢谢!
...全文
101 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ohd 2008-08-26
  • 打赏
  • 举报
回复
删除\插入其实都是重写操作.
skigil 2008-08-26
  • 打赏
  • 举报
回复
'SourceString为原文本中的字串,用readalltext读取
'AddString为将要加入的字串
'n为加入第几行?如为0,则加入最前面,如为1,则加在第一行后面.....
public function Add(SourceString,AddString,n as integer) as string
dim rString as string=string.empty
if n=0
rString=AddString+Vbcrlf+SourceString
else
dim Lstring() as string=SourceString.Split(VbCrlf)
for i as integer=0 to n-1
rString+=Lstring(i)+vbcrlf
next
rString+=AddString+vbcrlf
for i as integer=n to Lstring.length-1
rString+=Lstring(i)+vbcrlf
next
end if
return rString
end function
public function Del(....)
....'看官自己写,我这没有软件,就在IE里敲的,有什么错误原谅
end function
cauhorse 2008-08-25
  • 打赏
  • 举报
回复
太大了的确会存在一些问题,
但是如果想修改指定行,
还是得自已编写代码,
而且一般也是整读整取的方式。。。
LQknife 2008-08-25
  • 打赏
  • 举报
回复
先把文本文件按行存到数组,在你要插的行对应的数组对应的索引处插入一条
最后写入文件里
ojekleen 2008-08-25
  • 打赏
  • 举报
回复
重写是很正常的,IO里就连个重命名都得remove()
LYDF4151 2008-08-25
  • 打赏
  • 举报
回复
谁说不可以。
fs = New System.IO.FileStream(filename, IO.FileMode.OpenOrCreate)
路人乙e 2008-08-25
  • 打赏
  • 举报
回复
文本插入行只有一个办法(据我所知)——重写
yangpeiyu 2008-08-25
  • 打赏
  • 举报
回复

/// <summary>
/// 写文本内容
/// </summary>
/// <param name="StrFileName">文本名</param>
/// <param name="StrOutPutStr">写入内容</param>
public static bool WriteTxtFile(string StrFileName, string StrOutPutStr)
{
string StrPath = string.Empty; //定义存放目录变量
StrPath = Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath) + @"\Log\" + StrFileName + ".txt";

//判断是否有当前文件,如果有,则删除
try
{
if (File.Exists(StrPath))
{
//直接写内容文本
using (StreamWriter ObjStreamWriter = File.AppendText(StrPath))
{
ObjStreamWriter.WriteLine(StrOutPutStr);
}
}
else
{
//创建文件并写内容文本
using (StreamWriter ObjStreamWriter = File.CreateText(StrPath))
{
ObjStreamWriter.WriteLine(StrOutPutStr);
}
}
return true;
}
catch(Exception ex)
{
StrErrMessage = ex.Message;
return false;
}
}

/// <summary>
/// 读文本内容
/// </summary>
/// <param name="StrFileName">文本名</param>
/// <returns>返回文本内容</returns>
public static string RreadTxtFile(string StrFileName)
{
string StrPath = string.Empty; //定义存放目录变量
StrPath = Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath) + @"\Log\" + StrFileName + ".txt";

//判断是否有当前文件,如果有,则删除
if (File.Exists(StrPath))
{
//直接写内容文本
using (StreamReader ObjStreamReader = File.OpenText(StrPath))
{
return ObjStreamReader.ReadToEnd();
}
}
else
return null;
}
yanlongwuhui 2008-08-25
  • 打赏
  • 举报
回复
自己编码处理下,读取要插入位置前的信息,插入需要的信息,再写入插入位置后的信息。
Qpinguo 2008-08-25
  • 打赏
  • 举报
回复
似乎有点烦啊!

看大家的意思好像是
把文本文件的内容读取存于一个数组或是什么的,然后在数组里面修改好以后,再存回文本文件里面是么?

感觉如果文本文件比较小还好!大了的话会不会很慢啊!
greystar 2008-08-25
  • 打赏
  • 举报
回复
文本文件的话,好象没办法删除一行,追加一行是可以的.

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧