您的位置:首页 > 编程语言 > C#

如何有效的使用C#读取文件

2015-10-20 16:41 561 查看
如何有效的使用C#读取文件

通常我们读取一个文件使用如下的步骤:

1、声明并使用File的OpenRead实例化一个文件流对象,就像下面这样

FileStream fs = File.OpenRead(filename);

或者

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);

2、准备一个存放文件内容的字节数组,fs.Length将得到文件的实际大小,就像下面这样

byte[] data = new byte[fs.Length];

3、开始读了,调用一个文件流的一个方法读取数据到data数组中

fs.Read (data, 0, data.Length);

呵呵!我们只写了3句就可以把文件里面的内容原封不动的读出来,真是太简洁了!可以这段代码真的能像你预期的那样工作么?答案是:几乎可以!在大部分情况下上面的代码工作的很好,但是我们应该注意Read方法是有返回值的,既然有返回值那么一定有其道理,如果按照上面的写法完全可以是一个没有返回值的函数。我想返回值的目的是,为了给我们一个机会判断实际读取文件的大小,从而来判断文件是否已经完全读完。所以上面的代码不能保证我们一定读完了文件里面的所有字节(虽然在很多情况下是读完了)。下面的方法提供了一个比上面方法更安全的方法,来保证文件被完全读出

public static void SafeRead (Stream stream, byte[] data){

int offset=0;

int remaining = data.Length;

// 只要有剩余的字节就不停的读

while (remaining > 0){

int read = stream.Read(data, offset, remaining);

if (read <= 0)

throw new EndOfStreamException("文件读取到"+read.ToString()+"失败!");

// 减少剩余的字节数

remaining -= read;

// 增加偏移量

offset += read;

}

}

有些情况下你不知道流实际的长度比如:网络流。此时可以使用类似的方法读取流直到流里面的数据完全读取出来为止。我们可以先初始化一段缓存,再将流读出来的流信息写到内存流里面,就像下面这样:

public static byte[] ReadFully (Stream stream){

// 初始化一个32k的缓存

byte[] buffer = new byte[32768];

using (MemoryStream ms = new MemoryStream()){ //返回结果后会自动回收调用该对象的Dispose方法释放内存

// 不停的读取

while (true){

int read = stream.Read (buffer, 0, buffer.Length);

// 直到读取完最后的3M数据就可以返回结果了

if (read <= 0)

return ms.ToArray();

ms.Write (buffer, 0, read);

}

}

}

虽然上面的例子都比较简单,效果也不是很明显(大部分都是对的),也许你早就会了,没关系这篇文章本来就是写给初学者的。

下面的方法提供了一种使用指定缓存长度的方式读取流,虽然在很多情况下你可以直接使用Stream.Length得到流的长度,但是不是所有的流都可以得到。

public static byte[] Read2Buffer (Stream stream, int BufferLen){

// 如果指定的无效长度的缓冲区,则指定一个默认的长度作为缓存大小

if (BufferLen < 1){

BufferLen = 0x8000;

}

// 初始化一个缓存区

byte[] buffer = new byte[BufferLen];

int read=0; 

int block;

// 每次从流中读取缓存大小的数据,知道读取完所有的流为止

while ( (block = stream.Read(buffer, read, buffer.Length-read)) > 0){

// 重新设定读取位置

read += block;

// 检查是否到达了缓存的边界,检查是否还有可以读取的信息

if (read == buffer.Length){

// 尝试读取一个字节

int nextByte = stream.ReadByte();

// 读取失败则说明读取完成可以返回结果

if (nextByte==-1){

return buffer;

}

// 调整数组大小准备继续读取

byte[] newBuf = new byte[buffer.Length*2];

Array.Copy(buffer, newBuf, buffer.Length);

newBuf[read]=(byte)nextByte;

buffer = newBuf;// buffer是一个引用(指针),这里意在重新设定buffer指针指向一个更大的内存

read++;

}

}

// 如果缓存太大则使用ret来收缩前面while读取的buffer,然后直接返回

byte[] ret = new byte[read];

Array.Copy(buffer, ret, read);

return ret;

}

转载:http://www.cnblogs.com/smhy8187/articles/978480.html


c#从文件读取文本

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{   // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}


C#实现新建文件并写入内容

1 using System;
2 using System.IO;
3
4 namespace ConsoleApplication1
5 {
6     class Program
7     {
8         static void Main(string[] args)
9         {
10
11             // 创建文件
12             FileStream fs = new FileStream("test.doc", FileMode.Create, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
13             StreamWriter sw = new StreamWriter(fs); // 创建写入流
14             sw.WriteLine("bob hu"); // 写入Hello World
15             sw.Close(); //关闭文件
16
17
19         }
20     }
21 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: