C#應用Streamwriter翻開文件的辦法。本站提示廣大學習愛好者:(C#應用Streamwriter翻開文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用Streamwriter翻開文件的辦法正文
本文實例講述了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#法式設計有所贊助。