ioexample.cs

来自「学生注册管理系统」· CS 代码 · 共 46 行

CS
46
字号
using System;
using System.IO;

public class IOExample
{
  static void Main() {   
    FileStream fs;
    StreamReader srIn;
    StreamWriter sw;

    // Read operations should be placed in a try-catch block.
    try {
      // Create a FileStream and a StreamReader
      fs = new FileStream("data.dat", FileMode.Open );
      srIn = new StreamReader(fs);

      // Read the first line from the file. 
      string line = srIn.ReadLine();

      Console.WriteLine("line = "+line);
      srIn.Close();

      // Open a StreamWriter based on the same FileStream. 
//      fs = new FileStream("data.dat", FileMode.Open );
//      sw = new StreamWriter(fs);

      // write a line to the file
//      string newLine = "Not so different from you and me";
//      sw.WriteLine(newLine);

      // close the streams
//      fs.Close();
//      srIn.Close();
//      sw.Close();
    }
//    catch (FileNotFoundException fnfe) {
//Console.WriteLine("FileNotFoundException occurred: "+fnfe);
      // Perform exception handling details omitted.
//    }
    catch (IOException ioe) {
Console.WriteLine("IOException occurred: "+ioe.Message);
      // Perform exception handling details omitted.
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?