04.C#文件和流的操作

Program.cs文件:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {

            /*
                文件操作:
                    1.使用using System.IO;
                    2.

            */

            //1.创建一个文件
            string f1 = "D:\\hello.txt";
            if (!File.Exists(f1)) {
                File.Create(f1);
            }
            //2.删除文件
            //File.Delete(f1);

            //3.创建文件夹
            string f2 = "D:\\hello";
            //Directory.CreateDirectory(f2);
            //4.删除文件夹
            //Directory.Delete(f2);


            5.          
            //StreamWriter SWrite = new StreamWriter(f1);
            //SWrite.Write("hello c# I'am chinese 哈喽");//把数据写到文件中
            //SWrite.Close();//关闭流         
            //SWrite.Dispose();//释放流

            //StreamReader SReader = new StreamReader(f1);
            //string msg = SReader.ReadToEnd();//从文件中读取数据
            //Console.WriteLine(msg);
            //SReader.Close();
            //SReader.Dispose();


            6.文件流(FileMode.OpenOrCreate:如果存在就打开,不存在就创建)
            //FileStream fs2 = new FileStream("D:\\Program.cs", FileMode.OpenOrCreate);
            //StreamReader SReader2 = new StreamReader(fs2);
            //string msg2 = SReader2.ReadToEnd();//从文件中读取数据
            //Console.WriteLine("文件内容"+msg2);
            //SReader2.Close();
            //fs2.Close();
            //SReader2.Dispose();
            //fs2.Dispose();

            //7.读取流数据->字节数组
            FileStream fs3 = new FileStream("D:\\Program.cs", FileMode.OpenOrCreate);
            //新建字节数组
            Byte[] bytes = new Byte[fs3.Length];
            //把流数据读取到字节码中
            fs3.Read(bytes,0,(int)fs3.Length);
            //转码:按照一定的编码格式吧字节码转换成字符串
            //string msg3 = System.Text.Encoding.Default.GetString(bytes);                  //默认编码格式
            string msg3 = System.Text.Encoding.UTF8.GetString(bytes);                       //utf-8编码格式
            //string msg3 = System.Text.Encoding.GetEncoding("GB2312").GetString(bytes);    //GB2312编码格式
            Console.WriteLine(msg3);
            fs3.Close();
            fs3.Dispose();

            //8.写入数据->字节数组
            FileStream fs4 = new FileStream("D:\\Program.cs", FileMode.OpenOrCreate);
            string getStr = Console.ReadLine();
            Byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(getStr);
            fs4.Write(bMsg,0, bMsg.Length);
            fs4.Flush();
            fs4.Close();
            fs4.Dispose();


        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值