C#使用Streamwriter打開文件的方法
更新時間:2015年04月08日 14:31:47 作者:heishui
這篇文章主要介紹了C#使用Streamwriter打開文件的方法,涉及C#操作文件的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了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#程序設計有所幫助。
相關文章
C#線程委托BeginInvoke與EndInvoke的用法
這篇文章介紹了C#線程委托BeginInvoke與EndInvoke的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

