zoukankan      html  css  js  c++  java
  • 文件操作 流

    一定要注意:
          如果读流,写流时没有写 编码方式,则得到的是乱码! ! !


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.IO;
    using System.Text;


    public partial class 读写文件 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //1:
            //ReadFile();

            //2:
            //string sf = ReadFile("C:\\ReadAndWrite.txt");

            //string strFormate = string.Format(sf, "China", "中国");

            //WriteFile(strFormate, "C:\\CopyFromReadAndWrite.txt");

            //Response.Write("Success!");

            //3:
            FileOperator opter = new FileOperator();
           ArrayList list= opter.ReadFile();
           for (int i = 0; i < list.Count; i++)
           {
               Response.Write(list.ToString());
           }

           opter.WriteFile();
        }

        //读取文件
        public string ReadFile(string FilePath)
        {
            //如果在读文件的时候没有指定格式,则在写文件的时候会产生乱码
            StreamReader sr = new StreamReader(FilePath, Encoding.GetEncoding("gb2312"));
            string StrRead = sr.ReadToEnd();

            sr.Close();
            return StrRead;
        }

        //读文件
        public void ReadFile()
        {
            //所有的文件操作都必须放在try程序块中
            try
            {
                //第二步:建立流
                using (FileStream fs = File.Open("C:\\ReadAndWrite.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
                {
                    //第三步:声明byte数组
                    byte[] b = new byte[fs.Length];

                    //第四步:一次性读取文件,把内容存放在数组b中
                    fs.Read(b, 0, b.Length);

                    //第五步:把数组b转换为相应的字符串
                    string s = UTF8Encoding.Default.GetString(b);

                    Response.Write(s);

                    //最后,关闭流,释放资源
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        //写入文件
        public void WriteFile(string Content, string FileSavePath)
        {
            if (System.IO.File.Exists(FileSavePath))
            {
                System.IO.File.Delete(FileSavePath);
            }
            System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
            System.IO.StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));


            Byte[] bContent = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(Content);
            fs.Write(bContent, 0, bContent.Length);
            fs.Close();
            fs = null;
        }


        /// <summary>
        /// 文件操作类
        /// </summary>
        public class FileOperator
        {

            //写文件:
            public void WriteFile()
            {
                FileStream fs = new FileStream("C:\\ReadAndWrite.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs,Encoding.Default);
                //开始写入
                sw.Write("重新写入文件的内容!");
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }

            //读文件:
            public ArrayList ReadFile()
            {
                //读文件如果不指定文件类型,则会出现乱码.
                StreamReader objReader = new StreamReader("C:\\ReadAndWrite.txt",Encoding.Default);
                string sLine = "";
                ArrayList LineList = new ArrayList();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null && !sLine.Equals(""))
                    {
                        LineList.Add(sLine);
                    }
                }
                objReader.Close();
                return LineList;
            }

            //复制一首歌:
            public void CopySong()
            {
                //创建两个文件流 一个是源文件相关,另一个是要写入的文件
                FileStream fs = new FileStream(@"D:\KuGoo\爱得太多.wma", FileMode.Open);
                FileStream fs2 = new FileStream(@"D:\love.wma", FileMode.Create);

                //创建一个字节数组,作为两者之间的媒介
                //好比两个人拿苹果,这个字节数组就好比一个篮子,一个人作死的把苹果送到篮子里面,
                //而我就可以作死得拿苹果,通过这个媒介我们互不干扰,
                //不需要互相等待【她往篮子里面放了苹果我才可以去拿】,提高了效率
                byte[] data = new byte[1024];


                //创建两个缓冲流,与两个文件流相关联
                BufferedStream bs = new BufferedStream(fs);
                BufferedStream bs2 = new BufferedStream(fs2);


                //fs作死的读,fs2作死的写,直到fs没有字节可读fs2就不写了
                //好比,一个人作死的往篮子里面丢苹果,另一个人作死得往篮子里面拿苹果,直到篮子里面没有苹果拿了为止
                //即-->那个人没有苹果往篮子里面放了
                while (fs.Read(data, 0, data.Length) > 0)
                {
                    fs2.Write(data, 0, data.Length);
                    fs2.Flush();
                }


                //关闭流,好比两个人累了,都要休息 呵呵o(∩_∩)o...
                fs.Close();
                fs2.Close();
            }
        }
    }
  • 相关阅读:
    RS交叉表按照预定的节点成员排序
    Open DJ备份与恢复方案
    SQLServer2008备份时发生无法打开备份设备
    数据仓库备份思路
    SQLServer代理新建或者编辑作业报错
    Transfrom在64bit服务下面无法运行
    ActiveReport开发入门-图表的交互性
    ActiveReport开发入门-列表的交互性
    /etc/fstab 参数详解(转)
    CentOS7 查看硬盘情况
  • 原文地址:https://www.cnblogs.com/MySpace/p/1599736.html
Copyright © 2011-2022 走看看