Csharp/C Sharp/File Stream/Byte Read Write — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Byte-Oriented File input and output
using System;
using System.IO;
class ShowFile {
public static void Main(string[] args) {
int i;
FileStream fin;
try {
fin = new FileStream("test.cs", FileMode.Open);
} catch(FileNotFoundException exc) {
Console.WriteLine(exc.Message);
return;
} catch(IndexOutOfRangeException exc) {
Console.WriteLine(exc.Message + "\nUsage: ShowFile File");
return;
}
// read bytes until EOF is encountered
do {
try {
i = fin.ReadByte();
} catch(Exception exc) {
Console.WriteLine(exc.Message);
return;
}
if(i != -1) Console.Write((char) i);
} while(i != -1);
fin.Close();
}
}
Byte-Oriented: Write to a file
using System;
using System.IO;
class WriteToFile {
public static void Main(string[] args) {
FileStream fout;
try {
fout = new FileStream("test.txt", FileMode.Create);
} catch(IOException exc) {
Console.WriteLine(exc.Message + "\nError Opening Output File");
return;
}
// Write the alphabet to the file.
try {
for(char c = "A"; c <= "Z"; c++)
fout.WriteByte((byte) c);
} catch(IOException exc) {
Console.WriteLine(exc.Message + "File Error");
}
fout.Close();
}
}