分享
 
 
 

使用 .NET的IO(2)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

向字符串写入字符

下面的代码示例把从字符数组中指定位置开始的一定数目的字符写入现有的字符串。使用 StringWriter 完成此操作,如下所示。

[C#]

using System;

using System.IO;

using System.Text;

public class CharsToStr

{

public static void Main(String[] args)

{

// Create a StringBuilder object that can then be modified.

StringBuilder sb = new StringBuilder("Some number of characters");

// Define and initialize a character array from which characters

// will be read into the StringBuilder object.

char[] b = {' ','t','o',' ','w','r','i','t','e',' ','t','o','.'};

// Create a StringWriter and attach it to the StringBuilder object.

StringWriter sw = new StringWriter(sb);

// Write three characters from the array into the StringBuilder object.

sw.Write(b, 0, 3);

// Display the output.

Console.WriteLine(sb);

// Close the StringWriter.

sw.Close();

}

}

此示例阐释了使用 StringBuilder 来修改现有的字符串。请注意,这需要一个附加的 using 声明,因为 StringBuilder 类是 System.Text 命名空间的成员。另外,这是一个直接创建字符数组并对其进行初始化的示例,而不是定义字符串然后将字符串转换为字符数组。

此代码产生以下输出:

Some number of characters to

构成流

后备存储器是一个存储媒介,例如磁盘或内存。每个不同的后备存储器都实现其自己的流作为 Stream 类的实现。每个流类型也都从其给定的后备存储器读取字节并向其给定的后备存储器写入字节。连接到后备存储器的流叫做基流。基流具有的构造函数具有将流连接到后备存储器所需的参数。例如,FileStream 具有指定路径参数(指定进程如何共享文件的参数)等的构造函数。

System.IO 类的设计提供简化的流构成。可以将基流附加到一个或多个提供所需功能的传递流。读取器或编写器可以附加到链的末端,这样便可以方便地读取或写入所需的类型。

下面的代码示例在现有 MyFile.txt 的周围创建 FileStream 对象,以缓冲 MyFile.txt。(请注意,默认情况下缓冲 FileStreams。)然后,创建 StreamReader 以读取 FileStream 中的字符,FileStream 被作为 StreamReader 的构造函数参数传递到 StreamReader。ReadLine 进行读取,直到 Peek 发现不再有字符为止。

[C#]

using System;

using System.IO;

public class CompBuf {

private const string FILE_NAME = "MyFile.txt";

public static void Main(String[] args) {

if (!File.Exists(FILE_NAME)) {

Console.WriteLine("{0} does not exist!", FILE_NAME);

return;

}

FileStream fsIn = new FileStream(FILE_NAME, FileMode.Open,

FileAccess.Read, FileShare.Read);

// Create a reader that can read characters from the FileStream.

StreamReader sr = new StreamReader(fsIn);

// While not at the end of the file, read lines from the file.

while (sr.Peek()>-1) {

String input = sr.ReadLine();

Console.WriteLine (input);

}

sr.Close();

}

}

下面的代码示例在现有 MyFile.txt 的周围创建 FileStream 对象,以缓冲 MyFile.txt。(请注意,默认情况下缓冲 FileStreams。)然后,创建 BinaryReader 以读取 FileStream 中的字节,FileStream 被作为 BinaryReader 的构造函数参数传递到 BinaryReader。ReadByte 进行读取,直到 PeekChar 发现不再有字节为止。

[C#]

using System;

using System.IO;

public class ReadBuf {

private const string FILE_NAME = "MyFile.txt";

public static void Main(String[] args) {

if (!File.Exists(FILE_NAME)) {

Console.WriteLine("{0} does not exist!", FILE_NAME);

return;

}

FileStream f = new FileStream(FILE_NAME, FileMode.Open,

FileAccess.Read, FileShare.Read);

// Create a reader that can read bytes from the FileStream.

BinaryReader sr = new BinaryReader(f);

// While not at the end of the file, read lines from the file.

while (sr.PeekChar()>-1) {

byte input = sr.ReadByte();

Console.WriteLine (input);

}

sr.Close();

}

}

创建编写器

下面的代码示例创建了一个编写器,编写器是一个可以获取某些类型的数据并将其转换成可传递到流的字节数组的类。

[C#]

using System;

using System.IO;

public class MyWriter {

private Stream s;

public MyWriter(Stream stream) {

s = stream;

}

public void WriteDouble(double myData) {

byte[] b = BitConverter.GetBytes(myData);

// GetBytes is a binary representation of a double data type.

s.Write(b, 0, b.Length);

}

public void Close() {

s.Close();

}

}

在本示例中,您创建了一个具有构造函数的类,该构造函数带有流参数。从这里,您可以公开任何需要的 Write 方法。您必须将编写的所有内容都转换为 byte[]。在您获得 byte[] 之后,Write 方法将其写入流。

异步文件 I/O

同步 I/O 意味着在 I/O 操作完成之前,方法被阻塞,I/O 操作完成后,方法返回其数据。使用异步 I/O,用户可以调用 BeginRead 或 BeginWrite。主线程可以继续进行其他工作,稍后,用户将能够处理数据。另外,多个 I/O 请求可以被同时挂起。

要在此数据可用时得到通知,您可以调用 EndRead 或 EndWrite,传入与您发出的 I/O 请求对应的 IAsyncResult。您还可以提供回调方法,该回调方法应调用 EndRead 或 EndWrite 以计算出读取或写入了多少字节。当许多 I/O 请求被同时挂起时,异步 I/O 可以提供较好的性能,但通常要求对您的应用程序进行一些重要的调整以使其正常工作。

Stream 类支持在同一个流上混合使用同步和异步读取及写入,而不管操作系统是否允许。Stream 根据其同步实现提供默认的异步读取和写入操作的实现,而根据其异步实现提供默认的同步读取和写入操作的实现。

当实现 Stream 的派生类时,必须为同步或异步 Read 和 Write 方法之一提供实现。虽然允许重写 Read 和 Write,并且异步方法(BeginRead、EndRead、BeginWrite 和 EndWrite)的默认实现将和同步方法的实现一起工作,但这不能提供最有效的性能。与之相似,如果您提供了一个异步方法的实现,同步 Read 和 Write 方法也将正常工作,但是,如果您专门实现同步方法,性能通常会更好。ReadByte 和 WriteByte 的默认实现调用带有一个元素字节数组的同步 Read 和 Write 方法。当从 Stream 派生类时,如果有内部字节缓冲区,强烈建议重写这些方法以访问内部缓冲区,这样性能将得到显著提高。

连接到后备存储器的流重写同步或异步 Read 和 Write 方法之一,以获取默认情况下另一种方法的功能。如果流不支持异步或同步操作,实施者只需让适当的方法引发异常即可。

下面的示例是一个假设的批量图像处理器的异步实现,其后是同步实现的示例。本代码用于在目录中的每个文件上执行耗费 CPU 资源的操作。有关更多信息,请参阅 .NET 框架开发人员规范中的“.NET 异步编程模型”主题。

[C#]

using System;

using System.IO;

using System.Threading;

using System.Runtime.InteropServices;

public class BulkImageProcAsync

{

public const String ImageBaseName = "tmpImage-";

public const int numImages = 200;

public const int numPixels = 512*512;

// ProcessImage has a simple O(N) loop, and you can vary the number

// of times you repeat that loop to make the application more CPU-bound

// or more IO-bound.

public static int processImageRepeats = 20;

// Threads must decrement NumImagesToFinish, and protect

// their access to it through a mutex.

public static int NumImagesToFinish = numImages;

public static Object NumImagesMutex = new Object[0];

// WaitObject is signalled when all image processing is done.

public static Object WaitObject = new Object[0];

public class ImageStateObject

{

public byte[] pixels;

public int imageNum;

public FileStream fs;

}

public static void MakeImageFiles()

{

int sides = (int) Math.Sqrt(numPixels);

Console.Write("Making "+numImages+" "+sides+"x"+sides+" images... ");

byte[] pixels = new byte[numPixels];

for(int i=0; i<numPixels; i++)

pixels[i] = (byte) i;

for(int i=0; i<numImages; i++)

{

FileStream fs = new FileStream(ImageBaseName+i+".tmp", FileMode.Create,

FileAccess.Write, FileShare.None, 8192, false);

fs.Write(pixels, 0, pixels.Length);

FlushFileBuffers(fs.Handle);

fs.Close();

}

Console.WriteLine("Done.");

}

public static void ReadInImageCallback(IAsyncResult asyncResult)

{

ImageStateObject state = (ImageStateObject) asyncResult.AsyncState;

//Console.WriteLine("Image "+state.imageNum+" was read

"+(asyncResult.CompletedSynchronously ? "synchronously" :

"asynchronously"));

Stream stream = state.fs;//(Stream) asyncResult.AsyncObject;

int bytesRead = stream.EndRead(asyncResult);

if (bytesRead != numPixels)

throw new Exception("In ReadInImageCallback, got wrong number of bytes

from the image! got: "+bytesRead);

ProcessImage(state.pixels, state.imageNum);

stream.Close();

// Now write out the image.

// Using asynchronous IO here appears not to be best practice. It ends up

// swamping the threadpool, since the threadpool threads are blocked

// on IO requests that we've just queued to the threadpool.

FileStream fs = new FileStream(ImageBaseName+state.imageNum+".done",

FileMode.Create, FileAccess.Write, FileShare.None, 4096, false);

fs.Write(state.pixels, 0, numPixels);

fs.Close();

// This application model uses too much memory.

// Releasing memory as soon as possible is a good idea, especially global

// state.

state.pixels = null;

// Record that an image is done now.

lock(NumImagesMutex)

{

NumImagesToFinish--;

if (NumImagesToFinish==0)

{

Monitor.Enter(WaitObject);

Monitor.Pulse(WaitObject);

Monitor.Exit(WaitObject);

}

}

}

public static void ProcessImage(byte[] pixels, int imageNum)

{

Console.WriteLine("ProcessImage "+imageNum);

// Do some CPU-intensive operation on the image.

for(int i=0; i<processImageRepeats; i++)

for(int j=0; j<numPixels; j++)

pixels[j] += 1;

Console.WriteLine("ProcessImage "+imageNum+" done.");

}

public static void ProcessImagesInBulk()

{

Console.WriteLine("Processing images... ");

long t0 = Environment.TickCount;

NumImagesToFinish = numImages;

AsyncCallback readImageCallback = new AsyncCallback(ReadInImageCallback);

for(int i=0; i<numImages; i++)

{

ImageStateObject state = new ImageStateObject();

state.pixels = new byte[numPixels];

state.imageNum = i;

// Very large items are read only once, so you can make the

// buffer on the file stream very small to save memory.

FileStream fs = new FileStream(ImageBaseName+i+".tmp",

FileMode.Open, FileAccess.Read, FileShare.Read, 1, true);

state.fs = fs;

fs.BeginRead(state.pixels, 0, numPixels, readImageCallback, state);

}

// Determine whether all images are done being processed.

// If not, block until all are finished.

bool mustBlock = false;

lock (NumImagesMutex)

{

if (NumImagesToFinish > 0)

mustBlock = true;

}

if (mustBlock)

{

Console.WriteLine("All worker threads are queued... Blocking until they

complete. numLeft: "+NumImagesToFinish);

Monitor.Enter(WaitObject);

Monitor.Wait(WaitObject);

Monitor.Exit(WaitObject);

}

long t1 = Environment.TickCount;

Console.WriteLine("Total time processing images: {0} ms", (t1-t0));

}

public static void Cleanup()

{

for(int i=0; i<numImages; i++)

{

File.Delete(ImageBaseName+i+".tmp");

File.Delete(ImageBaseName+i+".done");

}

}

public static void TryToClearDiskCache()

{

// Try to force all pending writes to disk, and clear the

// disk cache of any data.

byte[] bytes = new byte[100*(1<<20)];

for(int i=0; i<bytes.Length; i++)

bytes[i] = 0;

bytes = null;

GC.Collect();

Thread.Sleep(2000);

}

public static void Main(String[] args)

{

Console.WriteLine("Bulk image processing sample application, using

async IO");

Console.WriteLine("Simulates applying a simple transformation to

"+numImages+" \"images\"");

Console.WriteLine("(ie, Async FileStream & Threadpool benchmark)");

Console.WriteLine("Warning - this test requires "+(numPixels *

numImages * 2)+" bytes of tmp space");

if (args.Length==1)

{

processImageRepeats = Int32.Parse(args[0]);

Console.WriteLine("ProcessImage inner loop -

"+processImageRepeats);

}

MakeImageFiles();

TryToClearDiskCache();

ProcessImagesInBulk();

Cleanup();

}

[DllImport("KERNEL32", SetLastError=true)]

private static extern void FlushFileBuffers(IntPtr handle);

}

以下是同一假设的同步示例。

[C#]

using System;

using System.IO;

using System.Threading;

using System.Runtime.InteropServices;

public class BulkImageProcSync

{

public const String ImageBaseName = "tmpImage-";

public const int numImages = 200;

public const int numPixels = 512*512;

// ProcessImage has a simple O(N) loop, and you can vary the number

// of times you repeat that loop to make the application more CPU-bound or

// more IO-bound.

public static int processImageRepeats = 20;

public static void MakeImageFiles()

{

int sides = (int) Math.Sqrt(numPixels);

Console.Write("Making "+numImages+" "+sides+"x"+sides+" images... ");

byte[] pixels = new byte[numPixels];

for(int i=0; i<numPixels; i++)

pixels[i] = (byte) i;

for(int i=0; i<numImages; i++) {

FileStream fs = new FileStream(ImageBaseName+i+".tmp", FileMode.Create, FileAccess.Write, FileShare.None, 8192, false);

fs.Write(pixels, 0, pixels.Length);

FlushFileBuffers(fs.Handle);

fs.Close();

}

Console.WriteLine("Done.");

}

public static void ProcessImage(byte[] pixels, int imageNum)

{

Console.WriteLine("ProcessImage "+imageNum);

// Do some CPU-intensive operation on the image

for(int i=0; i<processImageRepeats; i++)

for(int j=0; j<numPixels; j++)

pixels[j] += 1;

Console.WriteLine("ProcessImage "+imageNum+" done.");

}

public static void ProcessImagesInBulk()

{

Console.WriteLine("Processing images... ");

long t0 = Environment.TickCount;

byte[] pixels = new byte[numPixels];

for(int i=0; i<numImages; i++)

{

FileStream input = new FileStream(ImageBaseName+i+".tmp", FileMode.Open,

FileAccess.Read, FileShare.Read, 4196, false);

input.Read(pixels, 0, numPixels);

input.Close();

ProcessImage(pixels, i);

FileStream output = new FileStream(ImageBaseName+i+".done",

FileMode.Create, FileAccess.Write, FileShare.None, 4196, false);

output.Write(pixels, 0, numPixels);

output.Close();

}

long t1 = Environment.TickCount;

Console.WriteLine("Total time processing images: {0} ms", (t1-t0));

}

public static void Cleanup()

{

for(int i=0; i<numImages; i++)

{

File.Delete(ImageBaseName+i+".tmp");

File.Delete(ImageBaseName+i+".done");

}

}

public static void TryToClearDiskCache()

{

byte[] bytes = new byte[100*(1<<20)];

for(int i=0; i<bytes.Length; i++)

bytes[i] = 0;

bytes = null;

GC.Collect();

Thread.Sleep(2000);

}

public static void Main(String[] args)

{

Console.WriteLine("Bulk image processing sample application, using synchronous IO");

Console.WriteLine("Simulates applying a simple transformation to "+numImages+" \"images\"");

Console.WriteLine("(ie, Sync FileStream benchmark)");

Console.WriteLine("Warning - this test requires "+(numPixels * numImages * 2)+" bytes of tmp space");

if (args.Length==1) {

processImageRepeats = Int32.Parse(args[0]);

Console.WriteLine("ProcessImage inner loop - "+processImageRepeats);

}

MakeImageFiles();

TryToClearDiskCache();

ProcessImagesInBulk();

Cleanup();

}

[DllImport("KERNEL32", SetLastError=true)]

private static extern void FlushFileBuffers(IntPtr handle);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有