using System;
using System.IO;
//缓冲流
class MyStream
{
public static void Main(String[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Must include file name.");
}
else
{
FileStream myFile = new FileStream(args[0], FileMode.CreateNew);
BufferedStream myBuff = new BufferedStream(myFile, 1024);
// Write data to Test.data.
for (byte i = 0; i < 100; i++)
{
myBuff.WriteByte(i);
}
myBuff.Close();
myFile.Close();
FileStream myFile1 = new FileStream(args[0], FileMode.Open);
// Read data
int by;
Console.WriteLine("Reading file....");
while ((by = myFile1.ReadByte()) != -1) //myFile1.ReadByte() retur int 字节
{
Console.Write("<{0}> ", by);
}
Console.WriteLine("....Done Reading.");
myFile.Close();
}
}
}
// Write and then read back binary data.二进制流
using System;
using System.IO;
class RWData {
public static void Main() {
BinaryWriter dataOut;
BinaryReader dataIn;
int i = 10;
double d = 1023.56;
bool b = true;
try {
dataOut = new
BinaryWriter(new FileStream("testdata", FileMode.Create));
}
catch(IOException exc) {
Console.WriteLine(exc.Message + "\nCannot open file.");
return;
}
try {
Console.WriteLine("Writing " + i);
dataOut.Write(i);
Console.WriteLine("Writing " + d);
dataOut.Write(d);
Console.WriteLine("Writing " + b);
dataOut.Write(b);
Console.WriteLine("Writing " + 12.2 * 7.4);
dataOut.Write(12.2 * 7.4);
}
catch(IOException exc) {
Console.WriteLine(exc.Message + "\nWrite error.");
}
dataOut.Close();
Console.WriteLine();
// Now, read them back.
try {
dataIn = new
BinaryReader(new FileStream("testdata", FileMode.Open));
}
catch(FileNotFoundException exc) {
Console.WriteLine(exc.Message + "\nCannot open file.");
return;
}
try {
i = dataIn.ReadInt32();
Console.WriteLine("Reading " + i);
d = dataIn.ReadDouble();
Console.WriteLine("Reading " + d);
b = dataIn.ReadBoolean();
Console.WriteLine("Reading " + b);
d = dataIn.ReadDouble();
Console.WriteLine("Reading " + d);
}
catch(IOException exc) {
Console.WriteLine(exc.Message + "Read error.");
}
dataIn.Close();
}
}
using System;//字符流
using System.IO;
class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}
using System;
using System.IO;
public class TextToFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sw.Close();
}
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
using System;
using System.IO;
public class TextFromFile
{
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;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}
评论0