最后的方法
internal void modify_byte_jcw(string name, string path, string path2) { byte[] byData = null; char[] charData = null; FileStream aFile = null; try { aFile = new FileStream(path + @"/" + path2, FileMode.Open); byData = new byte[aFile.Length]; charData = new Char[aFile.Length]; aFile.Seek(0, SeekOrigin.Begin); aFile.Read(byData, 0, (int)aFile.Length); } catch (IOException e) { MessageBox.Show(e.ToString()); return; } Decoder d = Encoding.UTF8.GetDecoder(); d.GetChars(byData, 0, byData.Length, charData, 0); StringBuilder sb = new StringBuilder(256); sb.Append("\r\r\n\t"); sb.Append("<project>"); sb.Append("\r\r\n\t"); sb.Append("<path>" + name + @"\" + name + ".jcp</path>"); sb.Append("\r\r\n\t"); sb.Append("</project>"); sb.Append("\r\r\n\t"); sb.Append("</projects>"); sb.Append("\r\r\n\t"); char[] charData2 = new char[sb.Length]; charData2 = sb.ToString().ToCharArray();; byte[] byData2 = new byte[charData2.Length]; string a = ""; FileStream wFile = null; try { wFile = new FileStream(path + @"/temp.jcw", FileMode.Create); wFile.Seek(0, SeekOrigin.Begin); int num = 0; for (int i = 0; i < charData.Length; i++) { a = a + charData[i]; if (a.Contains("</projects>")) { wFile.Write(byData, 0, i - 12); Encoder e = Encoding.UTF8.GetEncoder(); e.GetBytes(charData2, 0, charData2.Length, byData2, 0, true); wFile.Write(byData2, 0, byData2.Length); a = ""; num = i; break; } } wFile.Write(byData, num + 3, byData.Length - num - 3); } catch (Exception e) { MessageBox.Show(e.ToString()); return; } finally { aFile.Close(); wFile.Close(); } }
Filestream读写文件总结
http://www.cnblogs.com/Fskjb/archive/2010/03/12/1684753.html
别人的c#读写文件的总结
http://www.cnblogs.com/qiantuwuliang/archive/2009/03/12/1409417.html
stream写文件的方式
internal void creatfile_xml_jcu(string name, string path, string path2) { //MessageBox.Show(); try { StreamWriter sw = File.AppendText(path + "/" + path2 + ".jcu");//这是你要建立的XML路径,可以不存在 sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); sw.Write("\r\r\n\r\r\n"); sw.Write("<workspace_user>"); sw.Write("\r\r\n\t"); sw.Write("<active_project>"); sw.Write("\r\r\n\t\t"); sw.Write("<label>" + path2 + "</label>"); sw.Write("\r\r\n\t"); sw.Write("</active_project>"); sw.Write("\r\r\n\t"); sw.Write("<projects>"); sw.Write("\r\r\n\t"); sw.Write("<project>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<label>" + name + "</label>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<showfiles>false</showfiles>"); sw.Write("\r\r\n\t\t"); sw.Write("</project>"); sw.Write("\r\r\n\t"); sw.Write("</projects>"); sw.Write("\r\r\n\t"); sw.Write("<open_files>"); sw.Write("\r\r\n\t\t"); sw.Write("<docitem>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<path>"+name+@"\Program.java</path>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<source>true</source>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<index>0</index>"); sw.Write("\r\r\n\t\t\t"); sw.Write("<active>true</active>"); sw.Write("\r\r\n\t\t"); sw.Write("</docitem>"); sw.Write("\r\r\n\t"); sw.Write("</open_files>"); sw.Write("\r\r\n"); sw.Write("</workspace_user>"); sw.Flush(); sw.Close(); } catch (Exception ex) { MessageBox.Show("错误的输出" + ex.Message); } }
stream插入行的方式,并写入文件,stream读取数据可能会失真,所以最后放弃了这种方法
internal void modify_stream_jcw(string name, string path, string path2) { //MessageBox.Show(path + @"/" + path2); try { //使用StreamReader类来读取文件 StreamReader sr = new StreamReader(path + @"/" + path2); sr.BaseStream.Seek(0, SeekOrigin.Begin); //准备写temp文件 StreamWriter sw = File.AppendText(path + @"/temp.jcw"); StringBuilder sb = new StringBuilder(128); while (sb.Append(sr.ReadLine())!=null) { if (sb.ToString().Trim().Length == 0) break; if (!sb.ToString().Contains("</projects>")) { sw.Write(sb.ToString()); } else { sw.Write("<project>"); sw.Write("\r\r\n\t"); sw.Write("<path>" + name + @"\" + name + ".jcp</path>"); sw.Write("\r\r\n\t"); sw.Write("</project>"); sw.Write("\r\r\n\t"); sw.Write(sb.ToString()); } MessageBox.Show(sb.ToString()); sb.Remove(0,sb.Length); //sb.Append(sr.ReadLine()); } //关闭此StreamReader对象 //MessageBox.Show(sb.ToString()); sr.Close(); sw.Flush(); sw.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
浙公网安备 33010602011771号