public static void MergeManyShpFile(string[] filenames, string TargetFile) { BinaryReader binaryReader1 = null; FileStream fileStream1 = null; fileStream1 = new FileStream(filenames[0], 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 binaryWriter1 = null; // 首先判断,文件是否已经存在 if (File.Exists(TargetFile)) { File.Delete(TargetFile);// 如果文件已经存在,那么删除掉. } fileStream2 = new FileStream(TargetFile, FileMode.Create, FileAccess.Write, FileShare.None); binaryWriter1 = new BinaryWriter(fileStream2); //大端格式写入fileCode; ShpFunction.WriteShpHeader(binaryWriter1, newShpHeader1); //先将第一个文件的shpheader写入新文件中 while (binaryReader1.BaseStream.Position < binaryReader1.BaseStream.Length) { newShpBody1 = ShpFunction.ReadShpFileBody(binaryReader1); ShpFunction.WriteShpBody(binaryWriter1, newShpBody1); } int k = newShpBody1.recordNo;//记录第一个文件记录的编号 //读入并写入第一个文件 BinaryReader binaryReader = null; FileStream fileStream = null; ShpHeader newShpHeader = new ShpHeader();//定义header ShpFileBody newShpBody = new ShpFileBody(); for (int i = 1; i < filenames.Length; i++) { fileStream = new FileStream(filenames[i], FileMode.Open, FileAccess.Read, FileShare.None); binaryReader = new BinaryReader(fileStream); newShpHeader = ShpFunction.ReadShpHeader(binaryReader); newShpHeader1 = ShpFunction.MergeShpHeader(newShpHeader, newShpHeader1); //合并文件头 while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { newShpBody = ShpFunction.ReadShpFileBody(binaryReader); newShpBody.recordNo = ++k;//通过循环将记录不断增加1 ShpFunction.WriteShpBody(binaryWriter1, newShpBody); } }//读取文件头和文件记录,将文件头合并,将文件记录读入 fileStream2.Position = 0; ShpFunction.WriteShpHeader(binaryWriter1, newShpHeader1); //后面的操作是将指针回到顶点,然后将合并后的shp头写入到新文件中 }
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 { // 最后关闭文件,无视关闭是否会发生错误了. } } } //合并逻辑 }