读取文本文件中的内容!!!!!!

tsinger 2004-06-23 10:13:56
我设定一个字符串数组,然后想循环把一个文本文件中的内容按行赋给数组的每一个成员。现在主要就是怎么循环读取文本文件中的每一行内容。
...全文
101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
CMIC 2004-06-23
  • 打赏
  • 举报
回复
System.IO.StreamReader mReader
=new System.IO.StreamReader("C:\\1.Txt",System.Text.Encoding.GetEncoding("gb2312"));

string mTempStr=mReader.ReadToEnd();
mReader.Close();
string[] mStrs=mTempStr.Split(new char[]{'\r','\n'});
我不懂电脑 2004-06-23
  • 打赏
  • 举报
回复

File 类提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 FileStream 对象。


using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Delete the file if it exists.
if (!File.Exists(path))
{
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");

// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}

// Open the stream and read it back.
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}

try
{
// Try to get another handle to the same file.
using (FileStream fs2 = File.Open(path, FileMode.Open))
{
// Do some task here.
}
}
catch (Exception e)
{
Console.Write("Opening the file twice is disallowed.");
Console.WriteLine(", as expected: {0}", e.ToString());
}
}
}
}

seeinrain 2004-06-23
  • 打赏
  • 举报
回复
string []ss = s.Split(new char[]{'\r','\n'});

然后删除空行就可以了

110,534

社区成员

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

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

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