C# 創(chuàng)建文本文件寫入讀取實現代碼
更新時間:2011年11月23日 21:46:43 作者:
C# 創(chuàng)建文本文件寫入讀取,可以用來做系統(tǒng)日志或程序操作日志或者錯誤記錄,需要的朋友可以參考下。
第一次運行時:
第二次運行時:
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 文件操作
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個文本文件,最好先判斷一下
StreamWriter sw;
if (!File.Exists("templog.txt"))
{
//不存在就新建一個文本文件,并寫入一些內容
sw = File.CreateText("templog.txt");
sw.Write("第一個字");
sw.WriteLine(" 跟隨老大的.");
sw.WriteLine("當前日期是:");
sw.WriteLine(DateTime.Now);
}
else
{
//如果存在就添加一些文本內容
sw = File.AppendText("templog.txt");
for (int i = 0; i < 10; i++)
{
sw.WriteLine("可以像平時輸出到屏幕一樣輸出{0}", i);
}
}
sw.Close();
//創(chuàng)建一個讀取器
StreamReader sr = new StreamReader("templog.txt");
//一次性讀取完
Console.WriteLine(sr.ReadToEnd());
Console.ReadLine();
}
}
}
相關文章
C# 微信支付 wx.chooseWXPay 簽名錯誤的解決方法
本篇文章主要介紹了C# 微信支付 wx.chooseWXPay 簽名錯誤的解決方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12