2016-03-21文件操作

本文介绍如何使用C#进行文件的读写操作,包括通过文件流进行读写的具体步骤,利用StreamReader和StreamWriter类实现内容的读取与写入,并展示了如何使用File类进行文件的复制、移动和删除等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读写文本文件

通过文件流

步骤:
1. 创建一个文件流
2. 创建相应的读写器
3. 执行读写操作
4. 关闭读写器
5. 关闭文件流 必须关闭,否则下次不能使用
* 引入命名空间
System.IO;
* 文件流 FileStream myfs = new FileStream( 文件路径, FileMode枚举类型选择方式,……);
* 读写器
StreamReader mySr = new StreamReader(fs); //读
StreamWriter sw = new StreamWriter(fs);//写
示例:

            //文件读写
            //写
            string path="a.txt";
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("这是一个写入文件的测试!");
                sw.Close();                
            }
            //读
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                Console.WriteLine(sr.ReadToEnd());
                sr.Close();
            }

            string path1 = @"C:\Users\dell\Desktop\01.txt";
            if (File.Exists(path1))//文件存在则读取
            {
                Console.WriteLine("下面是从{0}读取到的文件:",path1);
                string[] strall = File.ReadAllLines(path1);
                foreach(string s in strall )
                {
                    Console.WriteLine(s);
                }
             }
            else //不存在则新建并写入
            {
                string[] strNew = { "写入","这些信息","来","检查"};
                File.WriteAllLines(path1, strNew);
                Console.WriteLine("文件已经写入");
            }

文本文件可以不通过文件流读写

StreamWriter允许直接将字符串写入文件

        StreamWriter mySw = new StreamWriter(path);
        mySw.Write(content);
        mySw.Close();

StreamReader允许直接读取文件内容

            StreamReader mySr = new StreamReader(path);
            content = mySr.ReadToEnd();
            txtContent.Text = content;
            mySr.Close();

File类 文件操作

文件操作还包括拷贝、移动、删除等等,.NET提供一个File类,提供各种操作文件方法
* Exists(string path)
用于检查指定文件是否存在,该方法返回一个布尔值
* Copy(string SourceFilePath,string DestinationFilePath
按指定路径的源文件中的内容复制到目标文件中,如果目标文件不存在,则在指定路径中新建一个文件
* Move (string sourceFileName,string destFileName)
将指定文件移动到一个新的路径
* Delete(string path)
删除指定的文件,如果指定的文件不存在,则不引发异常

File类读写文件示例

        string path = @"d:\test.txt";            
        if (File.Exists(path))  //如果文件存在,则读取文件内容
        {
            //读取文件
            string[] content=File.ReadAllLines(path);
            Console.WriteLine("读取文件:");
            foreach (string s in content)
            {
                Console.WriteLine(s);
            }                
        }
        Else            //如果文件不存在,则新建文件并写入内容
        {
            //写入文件
            string[] content = {"Hello","And","Welcom"};
            File.WriteAllLines(path,content);
            Console.WriteLine("文件已写入!");
        } 

Directory类 目录操作

  • Exists(string path)
    用于检查指定文件夹在磁盘上是否存在
  • Move(string sourceDirName, string destDirName)
    用于将文件或目录及其内容移到新位置
  • Delete(string,Boolean)
    删除指定目录,如果bool指定true,则删除子目录中的所有目录内容
  • Delete(string path)
    删除指定的文件,如果指定的文件不存在,则不引发异常

静态类和非静态类

public static class Directory
public static class File
对于Directary和File类的系统定义都有static关键字,是静态类。

静态类:

  • 只包含静态成员,不能包含非静态成员
  • 不能被实例化
  • 不能包含实例构造函数

访问修饰符

成员访问权限

  • public 访问不受限制
  • internal 访问范围仅限于同一程序集
  • protected 本类和其子类中可以访问
  • protected internal 在同一程序集中可以访问,不同程序集中的子类可以访问
  • private 仅在本类中可以访问默认值

类的访问权限:

  • public 访问不受限制
  • sealed 密封类 不允许从他这儿继承
  • internal 访问范围仅限于同一程序集
order_date sales_region_code item_code first_cate_code second_cate_code sales_chan_name item_price ord_qty 2016-03-15 101 20001 302 408 offline 700 102 2016-03-21 101 20001 302 408 offline 705 19 2016-03-23 101 20001 302 408 offline 702 36 2016-03-24 101 20001 302 408 offline 692 204 2016-03-25 101 20001 302 408 offline 693 36 2016-05-06 101 20001 302 408 offline 707 305 2016-05-09 101 20001 302 408 offline 709 206 2017-08-04 101 20002 303 406 offline 1958 4 2018-03-14 101 20002 303 406 offline 2166 2 2018-03-16 101 20002 303 406 offline 2466 3 2018-03-25 101 20002 303 406 offline 2453 3 2018-03-31 101 20002 303 406 offline 2462 9 以上数据是excel表格,你能看出来吗 上表表格保存在test.xlsx文件:order_date(订单日期,注:订单日期从2015 年 9 月 1日至 2018 年 12 月 20 日)、sales_region_code(销售区域编码)、item_code(产品编码)、first_cate_code (产品大类编码)、second_cate_code (产品细类编码)、sales_chan_name (销售渠道名称)、item_price (产品价格)和 ord_qty (订单需求量) 希望给出下列描述的python代码。 读入表格,将数据转为模型可用的格式。 训练集与测试集的数据要转换成模型可使用的数据格式,数据维度要与模型的输入相匹配。 使用tensorflow创建lstm模型,其训练数据为相同的产品编码,对应的订单日期,对应的订单需求量。然后进行模型训练 模型训练完成后,对不同的产品编码预测其在2019年1月至3月每月的订单需求量。 需要将信息保存在result.xlsx表格中,其中应包含以下数据,产品编码,和对应的2019年1月订单需求量,2019年2月的需求量,2019年3月的需求量。
04-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值