public static string ShowString(string TextFileName) { BinaryReader binaryReader = null; FileStream fileStream = null; string allLine = null; try { fileStream = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader = new BinaryReader(fileStream); ShpHeader newShpHeader = new ShpHeader();//定义header newShpHeader = ShpFunction.ReadShpHeader(binaryReader); ShpFileBody newShpBody = new ShpFileBody(); allLine = allLine + ShpFunction.ShpHeader_to_String(newShpHeader); //大端格式读出记录编号,长度 while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { newShpBody = ShpFunction.ReadShpFileBody(binaryReader); allLine = allLine + ShpFunction.ShpBody_to_String(newShpBody); } ShpFunction.CloseReader(binaryReader, fileStream); } catch (Exception ex) { Console.WriteLine("在读取文件的过程中,发生了异常!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (fileStream != null) { try { fileStream.Close(); } catch { // 最后关闭文件,无视关闭是否会发生错误了. } } } return(allLine); }
public static void MergeShp(String TextFileName, String TextFileName1, String TextFileName2) { BinaryReader binaryReader = null; BinaryReader binaryReader1 = null; FileStream fileStream = null; FileStream fileStream1 = null; try { fileStream = new FileStream(TextFileName, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader = new BinaryReader(fileStream); ShpHeader newShpHeader = new ShpHeader();//定义header newShpHeader = ShpFunction.ReadShpHeader(binaryReader); ShpFileBody newShpBody = new ShpFileBody(); fileStream1 = new FileStream(TextFileName1, FileMode.Open, FileAccess.Read, FileShare.None); binaryReader1 = new BinaryReader(fileStream1); ShpHeader newShpHeader1 = new ShpHeader();//定义header newShpHeader1 = ShpFunction.ReadShpHeader(binaryReader1); ShpFileBody newShpBody1 = new ShpFileBody(); //写入新文件 FileStream fileStream2 = null; BinaryWriter binaryWriter = null; // 首先判断,文件是否已经存在 if (File.Exists(TextFileName2)) { File.Delete(TextFileName2);// 如果文件已经存在,那么删除掉. } fileStream2 = new FileStream(TextFileName2, FileMode.Create, FileAccess.Write, FileShare.None); binaryWriter = new BinaryWriter(fileStream2); //大端格式写入fileCode; newShpHeader = ShpFunction.MergeShpHeader(newShpHeader, newShpHeader1); //合并文件头 ShpFunction.WriteShpHeader(binaryWriter, newShpHeader); while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { newShpBody = ShpFunction.ReadShpFileBody(binaryReader); ShpFunction.WriteShpBody(binaryWriter, newShpBody); } while (binaryReader1.BaseStream.Position < binaryReader1.BaseStream.Length) { newShpBody1 = ShpFunction.ReadShpFileBody(binaryReader1); newShpBody1.recordNo = newShpBody1.recordNo + newShpBody.recordNo; ShpFunction.WriteShpBody(binaryWriter, newShpBody1); } //大端格式读出记录编号,长度 ShpFunction.CloseReader(binaryReader, fileStream); ShpFunction.CloseReader(binaryReader1, fileStream1); ShpFunction.CloseWriter(binaryWriter, fileStream2); } catch (Exception ex) { Console.WriteLine("在读取文件的过程中,发生了异常!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (fileStream != null) { try { fileStream.Close(); } catch { // 最后关闭文件,无视关闭是否会发生错误了. } } } //合并逻辑 }