最后的方法
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(""); sb.Append("\r\r\n\t"); sb.Append(" "); sb.Append("\r\r\n\t"); sb.Append(""); 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("")) { 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(); } }" + name + @"\" + name + ".jcp "); sb.Append("\r\r\n\t"); sb.Append("
Filestream读写文件总结
http://www.cnblogs.com/Fskjb/archive/2010/03/12/1684753.html
别人的c#读写文件的总结
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(" "); sw.Write("\r\r\n\r\r\n"); sw.Write(""); sw.Write("\r\r\n\t"); sw.Write(" "); sw.Flush(); sw.Close(); } catch (Exception ex) { MessageBox.Show("错误的输出" + ex.Message); } }"); sw.Write("\r\r\n\t\t"); sw.Write(" "); sw.Write("\r\r\n\t"); sw.Write(" "); sw.Write("\r\r\n\t"); sw.Write(""); sw.Write("\r\r\n\t"); sw.Write(" "); sw.Write("\r\r\n\t"); sw.Write(""); sw.Write("\r\r\n\t\t\t"); sw.Write(" "); sw.Write("\r\r\n\t\t\t"); sw.Write(" "); sw.Write("\r\r\n\t"); sw.Write("false "); sw.Write("\r\r\n\t\t"); sw.Write(""); sw.Write("\r\r\n\t\t"); sw.Write(" "); sw.Write("\r\r\n"); sw.Write(""); sw.Write("\r\r\n\t\t\t"); sw.Write(" "); sw.Write("\r\r\n\t"); sw.Write(""+name+@"\Program.java "); sw.Write("\r\r\n\t\t\t"); sw.Write("
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("")) { sw.Write(sb.ToString()); } else { sw.Write(""); sw.Write("\r\r\n\t"); sw.Write(" "); 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()); } }" + name + @"\" + name + ".jcp "); sw.Write("\r\r\n\t"); sw.Write("