// http://svc.luckstar.com.tw/CodeHelper/cs/index.html // 2017-04-14, honda@luckstar.com.tw, Create. // related keyword: // StreamWriter, UTF8Encoding, ReadLine, EndOfStream, StreamReader, FileStream, PadLeft, PadRight, Len, SubString. using System; using System.Collections.Generic; using System.Linq; using System.Text; // add using System.IO; namespace ConsoleBase { public class CFileStream { public void Run() { FileInfo f1 = new FileInfo("f1.txt"); decimal dTotalAmt1 = 0; string sDashLine = "T ------ ------------------ ------------------"; Console.WriteLine(); Console.WriteLine("----------"); Console.WriteLine("Export fixed length columns of text file with UTF8Encoding for Chinese characters:"); Console.WriteLine("StreamWriter, UTF8Encoding:"); using (FileStream stream1 = new FileStream(f1.FullName, FileMode.Create, FileAccess.Write)) using (StreamWriter wr1 = new StreamWriter(stream1, new UTF8Encoding(false))) { wr1.WriteLine("T {0} {1} {2}", PadLeftC("No.", 6), PadRightC("Item", 20), PadLeftC("Amount", 16) ); wr1.WriteLine(sDashLine); for (int i = 0; i < 5; i++) { decimal dAmt = 7654321 * i + 971; dAmt = decimal.Round(dAmt / 7, 2); dTotalAmt1 += dAmt; wr1.WriteLine("D {0} {1} {2}", PadLeftC(i.ToString(), 6), PadRightC("中文品名" + dAmt.ToString(), 20, true), PadLeftC(dAmt.ToString("#,##0.00"), 16) ); } wr1.WriteLine(sDashLine); wr1.WriteLine("S {0} {1} {2}", PadLeftC(" ", 6), PadRightC("Total", 20), PadLeftC(dTotalAmt1.ToString("#,##0.00"), 16) ); } Console.WriteLine("output file {0} as below:", f1.Name); Console.WriteLine(File.ReadAllText(f1.FullName)); Console.WriteLine(); Console.WriteLine("----------"); Console.WriteLine("Import fixed length columns of text file with UTF8Encoding for Chinese characters:"); Console.WriteLine("ReadLine, EndOfStream, StreamReader, FileStream:"); Console.WriteLine("No.,Item,Amt"); int iLengthDataLine = sDashLine.Length; int iCountData = 0; decimal dTotalAmt2 = 0; using (FileStream stream1 = new FileStream(f1.FullName, FileMode.Open, FileAccess.Read)) using (StreamReader rd1 = new StreamReader(stream1, new UTF8Encoding(false))) { int iLine; string sLine; string sH; string sNo; string sItem; string sAmt; int iNo; decimal dAmt; iLine = 0; while (!rd1.EndOfStream) { sLine = rd1.ReadLine(); iLine++; if (LenC(sLine, true) != iLengthDataLine) continue; sH = SubStringC(sLine, 0, 1, true).Trim(); sNo = SubStringC(sLine, 2, 6, true).Trim(); sItem = SubStringC(sLine, 9, 20, true).Trim(); sAmt = SubStringC(sLine, 30, 16, true).Trim(); if (sH == "D") iCountData++; else continue; if (!int.TryParse(sNo, out iNo)) iNo = 0; if (!decimal.TryParse(sAmt, out dAmt)) dAmt = 0; dTotalAmt2 += dAmt; Console.WriteLine("{0},{1},{2}", iNo, sItem, dAmt); } Console.WriteLine("dTotalAmt2={0}", dTotalAmt2.ToString("#,##0.00")); if (dTotalAmt1 == dTotalAmt2) Console.WriteLine("dTotalAmt1 == dTotalAmt2"); else Console.WriteLine("dTotalAmt1 != dTotalAmt2"); } } public static int LenC(string sInput) { return LenC(sInput, false); } public static int LenC(string sInput, bool bChinese) { if (bChinese) { return Encoding.GetEncoding(950).GetByteCount(sInput); } else { return sInput.Length; } } public static string PadRightC(string sInput, int iLen) { return PadRightC(sInput, iLen, ' ', false); } public static string PadRightC(string sInput, int iLen, Boolean bChinese) { return PadRightC(sInput, iLen, ' ', bChinese); } public static string PadRightC(string sInput, int iLen, char c1, Boolean bChinese) { if (bChinese) { return sInput + new String(c1, iLen - LenC(sInput, bChinese)); } else { return sInput.PadRight(iLen, c1); } } public static string PadLeftC(string sInput, int iLen) { return PadLeftC(sInput, iLen, ' ', false); } public static string PadLeftC(string sInput, int iLen, Boolean bChinese) { return PadLeftC(sInput, iLen, ' ', bChinese); } public static string PadLeftC(string sInput, int iLen, char c1, Boolean bChinese) { if (bChinese) { return new String(c1, iLen - LenC(sInput, bChinese)) + sInput; } else { return sInput.PadLeft(iLen, c1); } } public static string SubStringC(string sInput, int iStartIndex, int iLen) { return SubStringC(sInput, iStartIndex, iLen, false); } public static string SubStringC(string sInput, int iStartIndex, int iLen, bool bChinese) { if (bChinese) { return Encoding.GetEncoding(950).GetString(Encoding.GetEncoding(950).GetBytes(sInput), iStartIndex, iLen); } else { return sInput.Substring(iStartIndex, iLen); } } } } /* result: ---------- Export fixed length columns of text file with UTF8Encoding for Chinese characters: StreamWriter, UTF8Encoding: output file f1.txt as below: T No. Item Amount T ------ ------------------ ------------------ D 0 中文品名138.71 138.71 D 1 中文品名1093613.14 1,093,613.14 D 2 中文品名2187087.57 2,187,087.57 D 3 中文品名3280562 3,280,562.00 D 4 中文品名4374036.43 4,374,036.43 T ------ ------------------ ------------------ S Total 10,935,437.85 ---------- Import fixed length columns of text file with UTF8Encoding for Chinese characters: ReadLine, EndOfStream, StreamReader, FileStream: No.,Item,Amt 0,中文品名138.71,138.71 1,中文品名1093613.14,1093613.14 2,中文品名2187087.57,2187087.57 3,中文品名3280562,3280562.00 4,中文品名4374036.43,4374036.43 dTotalAmt2=10,935,437.85 dTotalAmt1 == dTotalAmt2 */