你比方说给出txt中3条如下所示的记录:
11111111:tttttttttttttttttttttt:rrrrrrrrrrrrrrrrrrrrrrrr?yyyyyyyyyyyyyyyyyyyyyyyyyy:ttttttttttt:yyyyyyyyyyyyyyyyyyyy:77777777:88888888:::::::::4444444
22222222:ttttttttttttttttttt:444444444444:%ppppppppp:66666666666666666666666666666666666666666666666666666666666
33333333:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
怎样提取出信息存在另一个txt中,信息如下:
11111111
22222222
33333333小弟不熟悉c#,麻烦请给出可行的代码,谢谢!!
11111111:tttttttttttttttttttttt:rrrrrrrrrrrrrrrrrrrrrrrr?yyyyyyyyyyyyyyyyyyyyyyyyyy:ttttttttttt:yyyyyyyyyyyyyyyyyyyy:77777777:88888888:::::::::4444444
22222222:ttttttttttttttttttt:444444444444:%ppppppppp:66666666666666666666666666666666666666666666666666666666666
33333333:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
怎样提取出信息存在另一个txt中,信息如下:
11111111
22222222
33333333小弟不熟悉c#,麻烦请给出可行的代码,谢谢!!
{
using(StreamWriter sw = new StreamWriter("你的新文件"))
while(sr.Peek() > 0)
{
string Data = sr.ReadLine();
sw.WriteLine(Data.Substring(0,8));
}
}
{
using(StreamWriter sw = new StreamWriter("你的新文件"))
while(sr.Peek() > 0)
{
string Data = sr.ReadLine();
sw.WriteLine(Data.Substring(0,8));
}
}
正解
运行显示
sw.WriteLine(Data.Substring(0, 8)); 未处理ArgumentOutOfRangeException 索引和长度必须引用该字符串内的位置。
参数名: length怎么回事儿啊??、
if(Data.Length >=8)
sw.WriteLine(Data.Substring(0,8));
PS:文本最后如果多加了几个换行符就会这样。
StreamReader str = new StreamReader(Server.MapPath("旧文件.txt"));
while (str.Peek() > 0)
{
string ss = str.ReadLine();
sw.WriteLine(ss.Substring(0,8));
}
sw.Close();
11111111:tttttttttttttttttttttt:rrrrrrrrrrrrrrrrrrrrrrrr?yyyyyyyyyyyyyyyyyyyyyyyyyy:ttttttttttt:yyyyyyyyyyyyyyyyyyyy:77777777:88888888:::::::::4444444
22222222:ttttttttttttttttttt:444444444444:%ppppppppp:66666666666666666666666666666666666666666666666666666666666
33333333:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
44444444:444444444444444444444444444444444444444444444444444"55555555555555555555555"^hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh //行番号
int iLine = 0;
try
{
string path = @"D:\test.txt";//读取文件txt
string savepath = @"D:\test1.txt";//存放文件txt
using (FileStream fs = new FileStream(path, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
iLine++;
string sLine = sr.ReadLine();
if (sLine.Length < 1)
{
continue;
}
if (iLine % 2 == 0)//隔行读取
{
string sRecordKbn = sLine.Substring(0, 8);//截取的数据
//另存到其他txt文件中
if (File.Exists(savepath))
{
File.AppendAllText(savepath, sRecordKbn + "\r\n");
}
}
}
}
}
}
catch (Exception ex)
{ Response.Write(ex.Message);
}
test1.txt
22222222
44444444
http://msdn.microsoft.com/zh-cn/library/system.io.file.appendtext(v=VS.80).aspx
详情参考以上网址。
File.AppendAllText(savepath, sRecordKbn + "\r\n");可以改成
using (StreamWriter sw = File.AppendText(savepath))
{
sw.WriteLine(sRecordKbn + "\r\n");
}
注释掉
int iLine = 0;
#region/////////////////////////第一种
string path = @"D:\test.txt";//读取文件txt
string savepath = @"D:\test1.txt";//存放文件txt
using (FileStream fs = new FileStream(path, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
iLine++;
string sLine = sr.ReadLine();
if (sLine.Length < 1)
{
continue;
}
if (iLine % 2 == 0)
{
string sRecordKbn = sLine.Substring(0, 8);//截取的数据
//另存到其他txt文件中
if (File.Exists(savepath))
{
using (StreamWriter sw = File.AppendText(savepath))
{
sw.WriteLine(sRecordKbn);
}
} }
}
}
}
#endregion
#region /////////////////////////第二种改正
using (StreamReader sr = new StreamReader(@"D:\test.txt"))//读取文件txt
{
while (!sr.EndOfStream)
{
iLine++;
string sLine = sr.ReadLine();
if (sLine.Length < 1)
{
continue;
}
if (iLine % 2 == 0)
{
string sRecordKbn = sLine.Substring(0, 8);//截取的数据
if (File.Exists(@"D:\test1.txt"))//提出数据存放文件txt
{
using (StreamWriter sw = File.AppendText(@"D:\test1.txt"))
{
sw.WriteLine(sRecordKbn);
}
}
else//文件不存在创建文件
{
FileStream fs;
fs = File.Create(@"D:\test1.txt");//创建不要用file创建
//使用File.Create创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件
//改用 FileStream 获取 File.Create 返回的 System.IO.FileStream 再进行关闭就无此问题
fs.Close();
using (StreamWriter sw = File.AppendText(@"D:\test1.txt"))
{
sw.WriteLine(sRecordKbn);
}
}
}
}
}
#endregion
看看那个适合你。用哪个吧
sw.WriteLine(Data.Substring(0,8));