標籤:比較 分析 路徑 使用 ... null pre 檔案目錄 完成
1字串與時間的互轉
DateTime.TryParse將Null 字元串、為null或格式不正確,則轉換為的DateTime所代表的值為:0001/1/1 0:00:00,此為DateTime.MinValue的值。
使用public static DateTime ParseExact(string s, string format, IFormatProvider provider);執行個體方法轉換時間,可以指定轉換的格式。其中format為輸入字元s的格式。若指定的格式與輸入字元格式設定不一樣,會拋異常
例如:
//dateTime ="2017-11-18 17:25:53";會拋異常,這種類型對應格式為:yyyy-MM-dd HH:mm:ss
string dateTime = "20171118172553";
IFormatProvider yyyymmddFormat = new CultureInfo(String.Empty, false);
DateTime time = DateTime.ParseExact(dateTime, "yyyyMMddHHmmss", yyyymmddFormat);
2比較時間先後
使用DateTime的public int CompareTo(DateTime value);執行個體方法。
樣本:
string dateTime = "2017-11-28 12:57:30";
DateTime dt;
DateTime.TryParse(dateTime, out dt);
int cr = dt.CompareTo(DateTime.Now);
如果cr大於0,dt時間晚於(目前時間)DateTime.Now,即時間的整數值大於DateTime.Now的整數值。
如果cr等於0,dt時間等於(目前時間)DateTime.Now,即時間的整數值等於DateTime.Now的整數值。
如果cr小於0,dt時間早於(目前時間)DateTime.Now,即時間的整數值小於DateTime.Now的整數值。
3 StreamWriter
將檔案所在的目錄傳給StreamWriter的建構函式,而不是傳遞檔案的全路徑,那麼會拋出如下的異常,看上去似乎是對檔案目錄沒有存取權限,其實是錯將檔案目錄傳遞給了StreamWriter建構函式。
4將Sream中的資料寫入檔案
錯誤的做法一:
擷取流的長度然後轉型為int,容易造成資料截斷,導致未能讀取流的全部內容。
//讀取 long length = 0; using (Stream fs = new FileStream("D:\\命令列安裝MySQL.docx", FileMode.Open)) { length = fs.Length; } byte[] bytes = new byte[length]; using (Stream fs = new FileStream("D:\\命令列安裝MySQL.docx", FileMode.Open)) { int countIn = fs.Read(bytes, 0, (int)length); } //寫入 using (Stream fs = new FileStream("D:\\命令列安裝MySQLnew.docx", FileMode.OpenOrCreate)) { fs.Write(bytes, 0, (int)length); }
錯誤做法二:
貌似不會發生錯誤類型一那樣的情況,但還是會出現資料截斷的現象。調用FileStream的Read執行個體方法:Read(byte[] buffer, int offset, int count),offset這個參數的類型是int型,這裡將numBytesRead 轉型為int,一旦這個位移量超過這個值,那麼讀取的資料有一部分就是重複的。
/讀取 using (Stream fs = new FileStream("D:\\命令列安裝MySQL.docx", FileMode.Open)) { int c = 10000; long numBytesRead = 0; long numBytesToRead = fs.Length; while (numBytesToRead>0) { if (fs.Length - numBytesRead <= c) { c = (int)(fs.Length - numBytesRead); } int n = fs.Read(bytes, (int)numBytesRead, c); numBytesRead += n; numBytesToRead -= n; } } //寫入 using (Stream fs = new FileStream("D:\\命令列安裝MySQLnew.docx", FileMode.OpenOrCreate)) { fs.Write(bytes, 0, (int)length); }
正確的做法一:
確保上述錯誤做法中的來源資料長度不超過int32的最大值。此外也可使用下面的方法,但同樣要保證來源資料長度不超過int32的最大值:
//讀取 using (Stream fs = new FileStream("D:\\命令列安裝MySQL.docx", FileMode.Open)) { int c = 10000; long position = 0; while (true) { position = fs.Seek(position, SeekOrigin.Begin); if (position == fs.Length) { break; } if (position + c > fs.Length) { c = (int)(fs.Length - position); } int n = fs.Read(bytes, (int)position, c); position += n; } } //寫入 using (Stream fs = new FileStream("D:\\命令列安裝MySQLnew.docx", FileMode.OpenOrCreate)) { fs.Write(bytes, 0, (int)length); }
正確的做法二:
使用CopyTo(Stream destination)
CopyTo(Stream destination, int bufferSize)
使用第一個方法預設緩衝區大小為4096,現在看下源碼片段:
public void CopyTo(Stream destination) { ...... InternalCopyTo(destination, _DefaultCopyBufferSize); }
CopyTo方法中調用了InternalCopyTo方法,來看下InternalCopyTo方法的源碼片段:
private void InternalCopyTo(Stream destination, int bufferSize) { ...... byte[] buffer = new byte[bufferSize]; int read; while ((read = Read(buffer, 0, buffer.Length)) != 0) destination.Write(buffer, 0, read); }
InternalCopyTo方法內部調用了Read方法,下面來看一下Read方法源碼片段:
然而在Stream這個類中並沒有Read方法的具體實現,只有一個抽象方法:
public abstract int Read([In, Out] byte[] buffer, int offset, int count);
到這裡已經可以看出CopyTo方法中的參數bufferSize的作用了,即設定記憶體緩衝區的大小,每次從流中讀取長度為bufferSize的資料,放入緩衝區,然後寫入目標流,重複這個過程直到所有的流都拷貝完成。那麼在記憶體允許的範圍內,bufferSize設定的越大效率越高。
using (Stream fs = new FileStream("D:\\命令列安裝MySQL.docx", FileMode.Open)) { using(Stream fss = new FileStream("D:\\命令列安裝MySQLnew.docx", FileMode.OpenOrCreate)) { fs.CopyTo(fss); } }
5檔案讀寫效率與對象頻繁開關的影響
寫檔案方式一:
public static void W() { for (int i = 0; i < 1000;i++ ) { string s = "sdfrgyjefgyhjdsfdfgfghgew"+i; using (StreamWriter sw = new StreamWriter(@"D:\g.txt", true)) { sw.Write(s); } } }
方式二:
public static void WT() { using (StreamWriter sw = new StreamWriter(@"D:\gT.txt", true)) { for (int i = 0; i < 1000; i++) { string s = "sdfrgyjefgyhjdsfdfgfghgew" + i; sw.Write(s); } } }
分析:
方式一每寫一次檔案,開關一次StreamWriter對象,而方式二將所有的字串都寫入檔案後才關閉StreamWriter執行個體。統計資料如下:
迴圈次數 |
方式一 |
方式二 |
100萬 |
21861ms |
260ms |
1000 |
231ms |
13ms |
C#要點補充