激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C# - C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單實(shí)用的TXT文本操作及日志框架詳解

C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單實(shí)用的TXT文本操作及日志框架詳解

2022-02-27 15:45殷慈航 C#

這篇文章主要給大家介紹了關(guān)于利用C#如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單實(shí)用的TXT文本操作及日志框架的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們一起來(lái)看看吧

前言

首先先介紹一下這個(gè)項(xiàng)目,該項(xiàng)目實(shí)現(xiàn)了文本寫入及讀取,日志寫入指定文件夾或默認(rèn)文件夾,日志數(shù)量控制,單個(gè)日志大小控制,通過(guò)約定的參數(shù)讓用戶可以用更少的代碼解決問(wèn)題。

1.讀取文本文件方法

使用:JIYUWU.TXT.TXTHelper.ReadToString(“文件物理路徑”)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static string ReadToString(string path)
 {
  try
  {
  LogLock.EnterReadLock();
  StreamReader sr = new StreamReader(path, Encoding.UTF8);
  StringBuilder sb = new StringBuilder();
  string line;
  while ((line = sr.ReadLine()) != null)
  {
   sb.AppendLine(line.ToString());
  }
  sr.Close();
  sr.Dispose();
  return sb.ToString();
  }
  catch (IOException e)
  {
  Console.WriteLine(e.ToString());
  return null;
  }
  finally
  {
  LogLock.ExitReadLock();
  }
 }

實(shí)現(xiàn)解析:

(1.為防止任務(wù)讀取當(dāng)我們進(jìn)行讀取時(shí)需要添加讀取鎖保證可以依次讀取,否則可能出現(xiàn)被占用異常。

(2.創(chuàng)建讀取流StreamReader(注意:由于會(huì)出現(xiàn)亂碼這里要改一下把默認(rèn)改為Encoding.UTF8),依次讀取每一行。

(3.讀取完成釋放資源。并解鎖。

2.寫入文本文件方法

(1.創(chuàng)建文本并寫入

使用:JIYUWU.TXT.TXTHelper.CreateWrite(“文件物理路徑”,“文本內(nèi)容”)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static bool CreateWrite(string path, string context)
 {
  bool b = false;
  try
  {
  LogLock.EnterWriteLock();
  FileStream fs = new FileStream(path, FileMode.Create);
  //獲得字節(jié)數(shù)組
  byte[] data = System.Text.Encoding.Default.GetBytes(context);
  //開(kāi)始寫入
  fs.Write(data, 0, data.Length);
  //清空緩沖區(qū)、關(guān)閉流
  fs.Flush();
  fs.Close();
  return b;
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.ToString());
  return b;
  }
  finally
  {
  LogLock.ExitWriteLock();
  }
 }

(2.在文本文件末尾追加寫入

使用:JIYUWU.TXT.TXTHelper.WriteAppend(“文件物理路徑”,“文本內(nèi)容”)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static bool WriteAppend(string path, string context)
 {
  bool b = false;
  try
  {
  LogLock.EnterWriteLock();
  FileStream fs = new FileStream(path, FileMode.Append);
  StreamWriter sw = new StreamWriter(fs);
  //開(kāi)始寫入
  sw.Write(context);
  //清空緩沖區(qū)
  sw.Flush();
  //關(guān)閉流
  sw.Close();
  fs.Close();
  return b;
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.ToString());
  return b;
  }
  finally
  {
  LogLock.ExitWriteLock();
  }
 }

(3.自動(dòng)判斷換行追加或創(chuàng)建文本

使用:JIYUWU.TXT.TXTHelper.CreateOrWriteAppendLine(“文件物理路徑”,“文本內(nèi)容”)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static bool CreateOrWriteAppendLine(string path, string context)
 {
  bool b = false;
  try
  {
  LogLock.EnterWriteLock();
  if (!File.Exists(path))
  {
   FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
   StreamWriter sw = new StreamWriter(fs);
   long fl = fs.Length;
   fs.Seek(fl, SeekOrigin.End);
   sw.WriteLine(context);
   sw.Flush();
   sw.Close();
   fs.Close();
   b = true;
  }
  else
  {
   FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
   StreamWriter sw = new StreamWriter(fs);
   long fl = fs.Length;
   fs.Seek(fl, SeekOrigin.Begin);
   sw.WriteLine(context);
   sw.Flush();
   sw.Close();
   fs.Close();
   b = true;
  }
  return b;
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.ToString());
  return b;
  }
  finally
  {
  LogLock.ExitWriteLock();
  }
 }

實(shí)現(xiàn)解析:

(1)為防止多任務(wù)讀取當(dāng)我們進(jìn)行讀取時(shí)需要添加讀取鎖保證可以依次寫入,否則可能出現(xiàn)被占用異常。

(2)創(chuàng)建文本流FileStream及寫入流StreamWriter,直接進(jìn)行數(shù)據(jù)寫入。

(3)讀取完成釋放資源。并解鎖。

3.寫入日志

使用:JIYUWU.TXT.TXTHelper.WriteLog(“文本內(nèi)容”,“單個(gè)文件大小(選填默認(rèn)1M)”,“目錄下文件數(shù)量(選填默認(rèn)20個(gè))”,“輸出目錄(選填默認(rèn)bin文件下)”)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public static void WriteLog(string content, int fileSize = 1, int fileCount = 20, string filePath = "")
 {
  try
  {
  if (!string.IsNullOrWhiteSpace(filePath))
  {
   logPath = filePath;
  }
  LogLock.EnterWriteLock();
  logPath = logPath.Replace("file:\\", "");//這里為了兼容webapi的情況
  string dataString = DateTime.Now.ToString("yyyy-MM-dd");
  string path = logPath + "\\MyLog";
  if (!Directory.Exists(path))
  {
   Directory.CreateDirectory(path);
   path += "\\";
   path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
   FileStream fs = new FileStream(path, FileMode.Create);
   fs.Close();
  }
  else
  {
   int x = System.IO.Directory.GetFiles(path).Count();
   path += "\\";
   Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();
   string[] filePathArr = Directory.GetFiles(path, "*.txt", SearchOption.TopDirectoryOnly);
   if (filePathArr.Length == 0)
   {
   string sourceFilePath = path;
   path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
   FileStream fs = new FileStream(path, FileMode.Create);
   fs.Close();
   filePathArr = Directory.GetFiles(sourceFilePath, "*.txt", SearchOption.TopDirectoryOnly);
   }
   for (int i = 0; i < filePathArr.Length; i++)
   {
   FileInfo fi = new FileInfo(filePathArr[i]);
   fileCreateDate[filePathArr[i]] = fi.CreationTime;
   }
   fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
   FileInfo fileInfo = new FileInfo(fileCreateDate.Last().Key);
   if (fileInfo.Length < 1024 * 1024 * fileSize)
   {
   path = fileCreateDate.Last().Key;
   }
   else
   {
   path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
   FileStream fs = new FileStream(path, FileMode.Create);
   fs.Close();
   }
   if (x > fileCount)
   {
   File.Delete(fileCreateDate.First().Key);
   }
 
  }
  FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
  StreamWriter sw = new StreamWriter(fs2);
  long fl = fs2.Length;
  fs2.Seek(fl, SeekOrigin.Begin);
  sw.WriteLine(DateTime.Now.ToString("hh:mm:ss") + "---> " + content);
  sw.Flush();
  sw.Close();
  fs2.Close();
  }
  catch (Exception ex)
  {
  Console.WriteLine(ex.ToString());
  }
  finally
  {
  LogLock.ExitWriteLock();
  }
 
 }

實(shí)現(xiàn)解析(以全部默認(rèn)參數(shù)為例說(shuō)明):

(1.為防止多任務(wù)進(jìn)行操作,于是對(duì)文檔加一個(gè)寫入鎖,否則可能出現(xiàn)被占用異常。

(2.檢測(cè)文件目錄是否已存在,不存在則創(chuàng)建目錄并創(chuàng)建日志文件,存在就判斷文件數(shù)量和大小,文件大小超過(guò)設(shè)置的值或默認(rèn)值就新建一個(gè)文本,文件數(shù)量超過(guò)默認(rèn)值或設(shè)置值就刪除最早的一個(gè)文件。

(3.寫入到指定文件。

(4.完成釋放資源。并解鎖。

項(xiàng)目框架就介紹到這里吧,后期還會(huì)將功能擴(kuò)展,不多說(shuō)了源碼地址:

c-txt-log.rar (可能存在沒(méi)有測(cè)到的bug,出現(xiàn)的問(wèn)題可以反饋給我,謝謝您的支持)。

問(wèn)題匯總:

bug1:程序包中讀取txt可能出現(xiàn)亂碼,讀取流中改一下把默認(rèn)改為Encoding.UTF8應(yīng)該就可以了。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:https://www.cnblogs.com/jiyuwu/archive/2018/07/28/9383193.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久蜜臀一区二区三区av | 日韩毛片一区二区三区 | 亚洲天堂ww| 嫩草影院在线观看网站成人 | 国产精品久久久久久久久久三级 | 99精品在线观看 | 青青青在线免费 | 99re66热这里只有精品8 | 国产日韩线路一线路二 | 国产做爰全免费的视频黑人 | 亚洲第一成人av | 91精品片 | 高清国产福利 | h视频免费在线观看 | 九九视频久久 | 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 在线观看免费毛片视频 | 国产精品一区免费在线观看 | 精品欧美一区二区精品久久久 | 伊人久操视频 | 国产精品久久国产精麻豆96堂 | 久久精品在线免费观看 | 在线成人精品视频 | 中文字幕在线观看成人 | 日日摸夜夜骑 | 亚洲欧美在线视频免费 | 中国美女一级黄色大片 | 一区二区三区日韩电影 | 久久国产精品91 | 国产不卡av在线 | 一级电影在线免费观看 | 亚洲人成综合第一网 | 国产亚洲高清在线精品不卡 | 蜜桃av鲁一鲁一鲁一鲁 | 国产在线精品区 | 久久精品小短片 | 99亚洲国产精品 | 久久久久一区 | 欧美性受xxx黑人xyx性爽 | 香蕉国产在线视频 | 亚洲精品aⅴ中文字幕乱码 欧美囗交 |