FileInof

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FileInfo类
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"D:\FileTest\1707A\aa.txt";
            string destPath = @"D:\FileTest\1707A\bb.txt";
            Test01(path,destPath);
        }


        static void Test01(string path,string destPath)
        {
            FileInfo fi = new FileInfo(path);
            //获取当前文件所在的目录
            Console.WriteLine(fi.Directory);
            //获取当前文件所在的目录
            Console.WriteLine(fi.DirectoryName);
            //判断文件是否是只读的
            Console.WriteLine(fi.IsReadOnly);
            //获取文件的大小 单位是字节
            Console.WriteLine(fi.Length);
            //类似拷贝  复制操作  如果目标目录下面有相同的文件 则会抛出异常
            //fi.CopyTo(destPath);
            //类似剪切操作
            //fi.MoveTo(@"D:\FileTest\1707A\YY\bb.txt");

            StreamWriter sw = fi.AppendText();
            sw.Write("abcdefg");
            //强制刷出缓冲区中的数据
            sw.Flush();
            //关闭流
            sw.Close();
               //-----------------------------------追加
                //-----------------------------------------
            FileStream ss = new FileStream(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\FANFAN.txt", FileMode.Create, FileAccess.ReadWrite ,FileShare.ReadWrite);
            string str = "67867";
            //将字符串转化为字节数组
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            //文件流写入
            ss.Write(bytes, 0, bytes.Length);
            //设置文件流位置
            ss.Position = 0;
            //新建字节组容器
            byte[] bytesss = new byte[ss.Length];
            //可读的最大字节数
            int Max = (int)ss.Length;
            //字节的偏移
            int offet = 0;
            while (Max > 0)
            {
                //将读到的字节压入容器中
                int n = ss.Read(bytesss, offet, Max);
                if (n == 0)
                    break;
                Max -= n;
                offet += n;
            }
            Decoder D = Encoding.UTF8.GetDecoder();
            Char[] char_date = new char[bytesss.Length];
            //将字节数组转化为字符数组 结果: 67867
            D.GetChars(bytesss, 0, bytesss.Length, char_date, 0);
            Console.WriteLine(char_date);
            ss.Flush();

            //-------------------------------------------StreamReader
            //利用using 不用释放流 
            FileStream fs = new FileStream(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\笔记.txt", FileMode.Create);
            
            StreamWriter str_w = new StreamWriter(fs,  Encoding.GetEncoding("gb2312"));
            string str_demo = "asdafggg";
            str_w.Write(str_demo);
            //清空缓存区
            str_w.Flush();
            //关闭流
            str_w.Close();
            fs.Close();

            using (StreamReader stream_r = new StreamReader(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\笔记.txt", Encoding.GetEncoding("gb2312")))
            {

                string lines;
                //读取文件中的内容
                while ((lines = stream_r.ReadLine()) != null)
                {
                    Console.WriteLine(lines);
                }
            }
               
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值