写入文件总结

复制代码
复制代码
方法二:
FileStream fs
=new FileStream(sf.FileName,FileMode.Create);

StreamWriter sw
=new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
fs.Close();
复制代码

方法一:
//实例化一个文件流
FileStream fs=new FileStream(sf.FileName,FileMode.Create);
//获得字节数组
byte[] date=new UTF8Encoding().GetBytes(this.textBox1.Text);
//开始写入
fs.Write(date,0,date.Length);
//清空缓冲区,关闭流
fs.Flush();
fs.Close();
 
复制代码
方法三:
FileStream fs
=new FileStream(sf.FileName,FileMode.Create);
BinaryWrite bw
=new BinaryWrite(fs);
bw.Write(textBox1.Text);
bw.Flush();
bw.Close();
fs.Close();




//备注:其中fs为SaveFileDialog的实例
复制代码
 
 
 
 ----2012-3-21
今天导师交给我的任务就是遍历数据 然后筛选每一行的第三个数据不是65535的,下面是我实现的代码:
private readonly static char[] split = new char[] { ',' };
 
        static void File()
        {
            try
            {
                using (StreamReader streamReader = new StreamReader(@"E:\result\result.csv", System.Text.Encoding.UTF8))
                {
                    string line;
                    while ((line = streamReader.ReadLine()) != null)
                    {
                        //判断第三个数据是否为65535
                        string[] str = line.Split(split);
                        //第三个数据不为65535 则把数据存储起来
                        if (!str[2].Equals("65535"))
                        {
                            try
                            {
                                using (StreamWriter streamWrite = new StreamWriter(@"E:\result\resultNew.csv", true, System.Text.Encoding.UTF8))
                                {
                                    streamWrite.WriteLine("{0},{1},{2}", str[0], str[1], str[2]);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                        }
                        else
                            Console.WriteLine("****{0},{1},{2}", str[0], str[1], str[2]);
                        System.Console.WriteLine(line);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
            }
 
 
 
            //System.Console.WriteLine(line);
        }
        static void Main(string[] args)
        {
            File();
        }

  

 
 
 
 
 
 
复制代码
posted @   小霖2012  阅读(306)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示