Stream
1732 ワード
詳細
ストリームには、次の3つの基本的な操作が含まれます.
読み取り-ストリームからデータ構造(バイト配列など)にデータを転送します.≪書込み|Write|Eas≫-データ・ソースからストリームにデータを転送します.
検索-ストリーム内の現在の位置を検索し、を変更します.
ストリームには、次の3つの基本的な操作が含まれます.
読み取り-ストリームからデータ構造(バイト配列など)にデータを転送します.≪書込み|Write|Eas≫-データ・ソースからストリームにデータを転送します.
検索-ストリーム内の現在の位置を検索し、を変更します.
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))
{
File.Delete(path);
}
// ,
using (FileStream fs = File.Create(path))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r
and this is on a new line");
AddText(fs, "\r
\r
The following is a subset of characters:\r
");
for (int i=1;i < 120;i++)
{
AddText(fs, Convert.ToChar(i).ToString());
}
}
// ,
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}