FileStream file1 = new FileStream(“C:\Lost Cards\BattelMap.txt”, FileMode.Open);
file1.Seek(0, SeekOrigin.Begin);
file1.Read(BattelMap.ByData, 0, 500);
Decoder a = Encoding.Default.GetDecoder();
a.GetChars(BattelMap.ByData, 0, BattelMap.ByData.Length, BattelMap.CharData, 0);
file1.Close();
FileStream file2 = new FileStream(“C:\Lost Cards\MAP001.txt”, FileMode.Open);
file2.Seek(0, SeekOrigin.Begin);
file2.Read(Map001.ByData, 0, 500);
Decoder b = Encoding.Default.GetDecoder();
b.GetChars(Map001.ByData, 0, Map001.ByData.Length, Map001.CharData, 0);
file2.Close();
FileStream file3 = new FileStream(“C:\Lost Cards\MAP002.txt”, FileMode.Open);
file3.Seek(0, SeekOrigin.Begin);
file3.Read(Map002.ByData, 0, 500);
Decoder c = Encoding.Default.GetDecoder();
c.GetChars(Map002.ByData, 0, Map002.ByData.Length, Map002.CharData, 0);
file3.Close();
FileStream file4 = new FileStream(“C:\Lost Cards\Menu.txt”, FileMode.Open);
file4.Seek(0, SeekOrigin.Begin);
file4.Read(MenuMap.ByData, 0, 500);
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(MenuMap.ByData, 0, MenuMap.ByData.Length, MenuMap.CharData, 0);
file4.Close();
FileStream file5 = new FileStream(“C:\Lost Cards\GrowthType.txt”, FileMode.Open);
file5.Seek(0, SeekOrigin.Begin);
file5.Read(GrowthTypeMap.ByData, 0, 500);
Decoder e = Encoding.Default.GetDecoder();
e.GetChars(GrowthTypeMap.ByData, 0, GrowthTypeMap.ByData.Length, GrowthTypeMap.CharData, 0);
file5.Close();
FileStream file6 = new FileStream(“C:\Lost Cards\Bag.txt”, FileMode.Open);
file6.Seek(0, SeekOrigin.Begin);
file6.Read(BagWindow.ByData, 0, 500);
Decoder f = Encoding.Default.GetDecoder();
f.GetChars(BagWindow.ByData, 0, BagWindow.ByData.Length, BagWindow.CharData, 0);
file6.Close();
————————————————————————————以上代码本人读取了6个TXT文件,求简化
file1.Seek(0, SeekOrigin.Begin);
file1.Read(BattelMap.ByData, 0, 500);
Decoder a = Encoding.Default.GetDecoder();
a.GetChars(BattelMap.ByData, 0, BattelMap.ByData.Length, BattelMap.CharData, 0);
file1.Close();
FileStream file2 = new FileStream(“C:\Lost Cards\MAP001.txt”, FileMode.Open);
file2.Seek(0, SeekOrigin.Begin);
file2.Read(Map001.ByData, 0, 500);
Decoder b = Encoding.Default.GetDecoder();
b.GetChars(Map001.ByData, 0, Map001.ByData.Length, Map001.CharData, 0);
file2.Close();
FileStream file3 = new FileStream(“C:\Lost Cards\MAP002.txt”, FileMode.Open);
file3.Seek(0, SeekOrigin.Begin);
file3.Read(Map002.ByData, 0, 500);
Decoder c = Encoding.Default.GetDecoder();
c.GetChars(Map002.ByData, 0, Map002.ByData.Length, Map002.CharData, 0);
file3.Close();
FileStream file4 = new FileStream(“C:\Lost Cards\Menu.txt”, FileMode.Open);
file4.Seek(0, SeekOrigin.Begin);
file4.Read(MenuMap.ByData, 0, 500);
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(MenuMap.ByData, 0, MenuMap.ByData.Length, MenuMap.CharData, 0);
file4.Close();
FileStream file5 = new FileStream(“C:\Lost Cards\GrowthType.txt”, FileMode.Open);
file5.Seek(0, SeekOrigin.Begin);
file5.Read(GrowthTypeMap.ByData, 0, 500);
Decoder e = Encoding.Default.GetDecoder();
e.GetChars(GrowthTypeMap.ByData, 0, GrowthTypeMap.ByData.Length, GrowthTypeMap.CharData, 0);
file5.Close();
FileStream file6 = new FileStream(“C:\Lost Cards\Bag.txt”, FileMode.Open);
file6.Seek(0, SeekOrigin.Begin);
file6.Read(BagWindow.ByData, 0, 500);
Decoder f = Encoding.Default.GetDecoder();
f.GetChars(BagWindow.ByData, 0, BagWindow.ByData.Length, BagWindow.CharData, 0);
file6.Close();
————————————————————————————以上代码本人读取了6个TXT文件,求简化
解决方案
1
循环+数组,代码都是重复的,或包装成函数
39
BattelMap、Map001、Map002、MenuMap…..
这些应该是某个自定义类的对象,假设这个类的类名 是 A
这些应该是某个自定义类的对象,假设这个类的类名 是 A
private void ReadTextAll(string txtPath, A a) { string[] txtPathStr = new string[] { "C:\Lost Cards\BattelMap.txt", "C:\Lost Cards\MAP001.txt", "C:\Lost Cards\MAP002.txt", "C:\Lost Cards\Menu.txt", "C:\Lost Cards\GrowthType.txt", "C:\Lost Cards\Bag.txt" }; A[] aStr = new A[] { BattelMap, Map001, Map002, MenuMap, GrowthTypeMap, BagWindow }; for (int i = 0; i < txtPathStr.Length; i++) { ReadText(txtPathStr[i], aStr[i]); } } private void ReadText(string txtPath, A a) { FileStream file1 = new FileStream(txtPath, FileMode.Open); file1.Seek(0, SeekOrigin.Begin); file1.Read(a.ByData, 0, 500); Decoder dec = Encoding.Default.GetDecoder(); dec.GetChars(a.ByData, 0, a.ByData.Length, a.CharData, 0); file1.Close(); }