C#如何删除TXT文件中的其中一行
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace po
{
class Program
{
static void Main(string[] args)
{
try
{
main();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("-------------------END-------------------");
Console.ReadLine();
}
}
public static void main()
{
del();
}
public static void del()
{
Console.Write("文件名:");
string source = Console.ReadLine();
List<string> lss = read(source);
begain:
Console.Write("要删除的行的关键字:");
string del = Console.ReadLine();
List<string> temp = new List<string>();
//把不包含关键字的行赋值给temp
int count = 0;
for (int i = 1; i < lss.Count; i++)
{
if (lss[i].Contains(del))
{
count++;
continue;
}
else
{
temp.Add(lss[i]);
}
}
Console.WriteLine("共搜索出"+count .ToString ()+"项包含"+del);
//如果还有其他关键字就把temp赋值给lss
Console.WriteLine("Press Y to continu , Press N to end this action");
if (Console.ReadLine() == "y")
{
lss = temp;
goto begain;
}
else
{
//把最终的版本写回到原文件中
write(source, temp);
}
}
public static List<string> read(string FilePath)
{
List<string> ls = new List<string>();
FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
while (!sr.EndOfStream)
{
ls.Add(sr.ReadLine());
}
sr.Close();
fs.Close();
return ls;
}
public static void write(string FilePath, List<string> ls)
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
FileStream fs = new FileStream(FilePath, FileMode.CreateNew, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
for (int i = 0; i < ls.Count; i++)
{
sw.WriteLine(ls[i]);
}
sw.Close();
fs.Close();
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace po
{
class Program
{
static void Main(string[] args)
{
try
{
main();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("-------------------END-------------------");
Console.ReadLine();
}
}
public static void main()
{
del();
}
public static void del()
{
Console.Write("文件名:");
string source = Console.ReadLine();
List<string> lss = read(source);
begain:
Console.Write("要删除的行的关键字:");
string del = Console.ReadLine();
List<string> temp = new List<string>();
//把不包含关键字的行赋值给temp
int count = 0;
for (int i = 1; i < lss.Count; i++)
{
if (lss[i].Contains(del))
{
count++;
continue;
}
else
{
temp.Add(lss[i]);
}
}
Console.WriteLine("共搜索出"+count .ToString ()+"项包含"+del);
//如果还有其他关键字就把temp赋值给lss
Console.WriteLine("Press Y to continu , Press N to end this action");
if (Console.ReadLine() == "y")
{
lss = temp;
goto begain;
}
else
{
//把最终的版本写回到原文件中
write(source, temp);
}
}
public static List<string> read(string FilePath)
{
List<string> ls = new List<string>();
FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
while (!sr.EndOfStream)
{
ls.Add(sr.ReadLine());
}
sr.Close();
fs.Close();
return ls;
}
public static void write(string FilePath, List<string> ls)
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
FileStream fs = new FileStream(FilePath, FileMode.CreateNew, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
for (int i = 0; i < ls.Count; i++)
{
sw.WriteLine(ls[i]);
}
sw.Close();
fs.Close();
}
}
}
展开全部
文件 C:\1.txt 内容如下:
a
NA
b
NA
c
d
e
处理完成后得到的C:\2.txt内容如下:
a
b
c
d
e
删除了带有"NA"的行:
代码如下:
System.IO.StreamReader srobj = new System.IO.StreamReader("C:\\1.txt");
System.IO.StreamWriter swobj = System.IO.File.AppendText("C:\\2.txt");
while (srobj.Peek() != -1)
{
bool neededline=true;
string linestring = srobj.ReadLine();
if (linestring.Contains("NA"))
{
neededline = false;
}
if(neededline==true)
{
swobj.WriteLine(linestring);
}
}
srobj.Close();
swobj.Flush();
swobj.Close();
a
NA
b
NA
c
d
e
处理完成后得到的C:\2.txt内容如下:
a
b
c
d
e
删除了带有"NA"的行:
代码如下:
System.IO.StreamReader srobj = new System.IO.StreamReader("C:\\1.txt");
System.IO.StreamWriter swobj = System.IO.File.AppendText("C:\\2.txt");
while (srobj.Peek() != -1)
{
bool neededline=true;
string linestring = srobj.ReadLine();
if (linestring.Contains("NA"))
{
neededline = false;
}
if(neededline==true)
{
swobj.WriteLine(linestring);
}
}
srobj.Close();
swobj.Flush();
swobj.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询