using System;
using System.IO;
// バイナリ形式でファイルに書き出し。
using(BinaryWriter bw = new BinaryWriter(File.OpenWrite(@"test\binary")))
{
bw.Write(new byte[]{(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67, });
bw.Write((int)123456789);
bw.Write((float)3.14159);
}
// バイナリ形式でファイルから呼び出し。
BinaryReader br = new BinaryReader(File.OpenRead(strFileName));
char [] cData= new char[4];
cData = br.ReadChars(4);
int iData = br.ReadInt32();
// ファイルにテキストを書き出し。
using(StreamWriter sw = new StreamWriter(@"test\test.txt"))
{
sw.WriteLine("基本的に、Console クラスの文字列出力メソッドと同じ。");
sw.WriteLine("WriteLine では末尾に改行文字が加えられます。");
int n = 5;
double x = 3.14;
sw.Write("書式指定出力もできます → n = {0}, x = {1}", n, x);
}
// ファイルからテキストを読み出し。
using(StreamReader sr = new StreamReader(@"test\test.txt"))
{
string line;
while( (line = sr.ReadLine()) != null) // 1行ずつ読み出し。
{
Console.WriteLine(line);
}
}
Tuesday, September 9, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment