FileStream读写文件问题

.Net技术 码拜 10年前 (2014-12-13) 1735次浏览 0个评论
我有两个方法,一个是读文件的(LoadList),另一个是写文件的(SaveList),但是一执行了LoadList读文件的,SaveList后面保存就失败了。哪位大哥帮忙看看哪里有错



        private void SaveList(string path)
        {
            string proxyText = "";

            foreach (ListViewItem lvi in listView1.Items)
            {
                proxyText += lvi.SubItems[0].Text + ":";
                proxyText += lvi.SubItems[1].Text + "\r\n";
            }
            
            FileStream fs = new FileStream(path,FileMode.Create);
            byte[] b = Encoding.UTF8.GetBytes(proxyText);
            fs.Write(b,0,b.Length);
            fs.Flush();
            fs.Close();

            //MessageBox.Show("已经自动保存" + Encoding.UTF8.GetBytes(proxyText));
        }


       private void LoadList(string path)
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }

            string proxyText = "";
            Regex reg = new Regex(regex);

            using (FileStream fs = File.OpenRead(path))
            {
                byte[] b = new byte[fs.Length];
                fs.Read(b, 0, b.Length);
                fs.Flush();
                fs.Close();

                proxyText = Encoding.UTF8.GetString(b);
                MatchCollection matches = reg.Matches(proxyText);
                for (int i = 0; i < matches.Count; i++)
                {
                    string proxyIpAndPort = matches[i].Groups[0].Value;

                    ListViewItem l = new ListViewItem();
                    l.SubItems[0].Text = proxyIpAndPort.Substring(0, proxyIpAndPort.IndexOf(':'));
                    l.SubItems.Add(proxyIpAndPort.Substring(proxyIpAndPort.IndexOf(':') + 1, proxyIpAndPort.Length - proxyIpAndPort.IndexOf(':') - 1));

                    listView1.Items.Add(l);

                }
            }
        }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明FileStream读写文件问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!