大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> 使用 .NET的IO(2) Paul_Ni(原作)

使用 .NET的IO(2) Paul_Ni(原作)

时间: 2021-07-31 作者:daque

向字符串写入字符

底下的代码示例把从字符数组中指定场所发端的确定数手段字符写入现有的字符串。运用 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 异步编制程序模子”中心。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.