已经知道文本中第几行,想用streamwriter把这行删除掉 不知道如何实现

zhizlm 2009-05-11 12:01:48
已经知道文本中第几行,想用streamwriter把这行删除掉 不知道如何实现

可以说是把这行删除掉 不留空格 不留空行 完全删除掉。请教
...全文
364 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhizlm 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 duping9626 的回复:]
ReadLine()一行一行读,然后跟过那一行,再写回去
[/Quote]


思路我也有。代码如何实现呢
duping9626 2009-05-11
  • 打赏
  • 举报
回复
跳过
duping9626 2009-05-11
  • 打赏
  • 举报
回复
ReadLine()一行一行读,然后跟过那一行,再写回去
zyk113229917 2009-05-11
  • 打赏
  • 举报
回复
StreamReader sd = File.OpenText("1.txt");
string savestr= sd.ReadToEnd();
sd.Close();
int foundS1 = name.IndexOf(" ");
int foundS2 = name.IndexOf(" ", foundS1 + 1);

if (foundS1 != foundS2 && foundS1 >= 0)
{

name = name.Remove(foundS1 + 1, foundS2 - foundS1);

}

StreamWriter sw = File.CreateText("1.txt");
sw.WriteLine(savestr);
sw.Close();

这是删除空格操作 自己写的 很容易看懂
kensouterry 2009-05-11
  • 打赏
  • 举报
回复
等会儿回来琢磨下,先把回复分拿到手,呵呵呵
diffmaker 2009-05-11
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CA1
{
class Program
{
static void Main(string[] args)
{
string file = "sample.txt";//文件名
string filecontent = string.Empty;
int line = 2;//不需要的行
int curline = 1;
using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding("GB2312")))
{
while (sr.Peek() != -1)
{
string tmp = sr.ReadLine();
if (line != curline)
{
if (filecontent != string.Empty) filecontent += "\r\n";
filecontent += tmp;
}
curline++;
}
}
//写回
using (StreamWriter sw = new StreamWriter(file, false, Encoding.GetEncoding("GB2312")))
{
sw.Write(filecontent);
}
Console.ReadLine();
}
}
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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