博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#文件读取
阅读量:5247 次
发布时间:2019-06-14

本文共 6451 字,大约阅读时间需要 21 分钟。

最后的方法

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("
" + name + @"\" + name + ".jcp
"); 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(); } }

 

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.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\t\t"); sw.Write("
"); sw.Write("\r\r\n\t\t\t"); sw.Write("
false
"); 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\t"); sw.Write("
"); sw.Write("\r\r\n\t\t\t"); sw.Write("
"+name+@"\Program.java
"); sw.Write("\r\r\n\t\t\t"); sw.Write("
true"); sw.Write("\r\r\n\t\t\t"); sw.Write("
0
"); sw.Write("\r\r\n\t\t\t"); sw.Write("
true
"); sw.Write("\r\r\n\t\t"); sw.Write("
"); sw.Write("\r\r\n\t"); sw.Write("
"); sw.Write("\r\r\n"); sw.Write("
"); 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(""))                    {                        sw.Write(sb.ToString());                    }                    else                    {                        sw.Write("
"); sw.Write("\r\r\n\t"); sw.Write("
" + name + @"\" + name + ".jcp
"); 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()); } }

 

转载于:https://www.cnblogs.com/cyangluiqin/archive/2013/03/23/2977690.html

你可能感兴趣的文章
一条简单的 SQL 执行超过 1000ms,纳尼?
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
Google Guava学习笔记——简介
查看>>
历时八年,HTML5 标准终于完工了
查看>>
17.树的子结构
查看>>
D - Mike and strings
查看>>
C++:多维数组的动态分配(new)和释放(delete)
查看>>
c#基础学习(0806)之抽象类实现多态
查看>>
S5PV210根文件系统的制作(一)
查看>>
51NOD 1244 莫比乌斯函数之和
查看>>
[bzoj1923]外星千足虫[高斯消元]
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
分析 PHP大马-php_mof SHELL
查看>>
TCP/IP
查看>>
[推荐] 协同滤波 —— Collaborative Filtering (CF)
查看>>