首页 > Python基础教程 >
-
vb.net教程之C#和VB.net语法对比图(2)
using System.IO;
//Write out to text file
StreamWriter writer = File.CreateText
("c:myfile.txt");
writer.WriteLine("Out to file.");
writer.Close();
//Read all lines from text file
StreamReader reader = File.OpenText
("c:myfile.txt");
string line = reader.ReadLine();
while (line != null) {
Console.WriteLine(line);
line = reader.ReadLine();
}
reader.Close();
//Write out to binary file
string str = "Text data";
int num = 123;
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite
("c:myfile.dat"));
binWriter.Write(str);
binWriter.Write(num);
binWriter.Close();
//Read from binary file
BinaryReader binReader = new BinaryReader(File.OpenRead
("c:myfile.dat"));
str = binReader.ReadString();
num = binReader.ReadInt32();
binReader.Close();