当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用Streamwriter打开文件的方法

C#使用Streamwriter打开文件的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#使用streamwriter打开文件的方法。分享给大家供大家参考。具体如下: using system; using system.io;

本文实例讲述了c#使用streamwriter打开文件的方法。分享给大家供大家参考。具体如下:

using system;
using system.io;
public class ktod1 {
 public static void main() {
  string str;
  streamwriter fstr_out;
  // open the file directly using streamwriter.
  try {
   fstr_out = new streamwriter("test.txt");
  }
  catch(ioexception exc) {
   console.writeline(exc.message + "cannot open file.");
   return ;
  }
  console.writeline("enter text ('stop' to quit).");
  do {
   console.write(": ");
   str = console.readline();
   if(str != "stop") {
    str = str + "\r\n"; // add newline
    try {
     fstr_out.write(str);
    } catch(ioexception exc) {
     console.writeline(exc.message + "file error");
     return ;
    }
   }
  } while(str != "stop");
  fstr_out.close();
 }
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网