一段老师写的,C#文件读取代码例子

2023-11-03


using System;
using System.IO;
using System.Collections;
namespace FileOperation
{
    public class TestFile
    {
        public static void MakeDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            // Console.WriteLine (strPath);
        }
        //创建一个文件夹在目录下
        public static void DeleteDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath, true);
            }
        }
        //删除以存在的文件夹
        public static void MoveAndCopyDirectory()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT/";
            string strPath2 = Directory.GetCurrentDirectory();
            strPath2 += "/kk/GT/";
            // Console.WriteLine (strPath);


            Directory.Move(strPath2, strPath);
        }
        //改变文件夹名称
        public static void ChooseTime()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/";
            if (Directory.Exists(strPath))
            {
                DateTime pTime = Directory.GetCreationTime(strPath);
                Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
                pTime = DateTime.Parse("2016/06/16 00:00:00");
                Console.WriteLine(pTime.ToString("yyyy/MM/dd hh:mm:ss"));
            }
        }
        //更改文件创建时间
        public static bool MakeFileEx(string strPath, ref byte[] pBuffer, bool isBinary = true)
        {
            bool isRet = false;
            FileStream pFile = null;
            const int nMaxWriteSize = 65535;
            do
            {
                if (File.Exists(strPath))
                {
                    break;
                }
                if (pBuffer == null || pBuffer.Length <= 0)
                {
                    break;
                }
                pFile = File.Create(strPath, pBuffer.Length, FileOptions.Asynchronous);
                if (pFile == null)
                {
                    break;
                }
                if (isBinary)
                {
                    BinaryWriter pWrite = new BinaryWriter(pFile);
                    if (pWrite == null)
                    {
                        break;
                    }
                    int nWriteCount = 0;
                    long nFileLen = pFile.Length;
                    while (nWriteCount < pBuffer.Length)
                    {
                        pWrite.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
                        pWrite.Flush();
                        nWriteCount += (int)(pFile.Length - nFileLen);
                    }
                }
                else
                {
                    int nWriteCount = 0;
                    long nFileLen = pFile.Length;
                    while (nWriteCount < pBuffer.Length)
                    {
                        pFile.Write(pBuffer, nWriteCount, nMaxWriteSize > pBuffer.Length - nWriteCount ? pBuffer.Length - nWriteCount : nMaxWriteSize);
                        pFile.Flush();
                        nWriteCount += (int)(pFile.Length - nFileLen);
                    }
                }
                isRet = true;
            } while (false);
            if (pFile != null)
            {
                pFile.Flush();
                pFile.Close();
            }
            return isRet;
        }


        public static string GetPath(string value)//获取路径
        {
            string strPath = Directory.GetCurrentDirectory();
            if (!value.Substring(0, 1).Equals("/"))
            {
                strPath += "/";
            }
            strPath += value;
            return strPath;
        }


        public static void MakeFile()//文件写入
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT";
            FileStream pFile = File.Create((strPath + "/MengBi.txt"), 100, FileOptions.Asynchronous);
            string strBuffer = "这是一个蒙蔽的程序猿蒙蔽的写入";
            byte[] pBuffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
            pFile.Write(pBuffer, 0, pBuffer.Length);
            pFile.Flush();
            pFile.Close();
        }
        //创建一个文本并写入


        public static bool TestRead()
        {
            string strPath = Directory.GetCurrentDirectory();
            strPath += "/kk/TT/1.txt";
            FileStream pFile = new FileStream(strPath, FileMode.Open);//读取文件
                                                                      //FileStream Mytxt = File.OpenRead(strPath);//第二种读取
            if (pFile == null)
            {
                Console.WriteLine("file open error!");
                return false;
            }
            byte[] pBuffer = new byte[(int)strPath.Length];//建立一个缓存池
            int ReadLen = pFile.Read(pBuffer, 0, pBuffer.Length);//把这个流读进缓冲池中去
            pFile.Close();
            if (ReadLen > 0)
            {
                string A = System.Text.Encoding.UTF8.GetString(pBuffer);
                Console.WriteLine("1.txt:{0}", A);
                return true;
            }
            return false;
        }




        public static bool TestReadEx(string strPath, bool isBinary = true)
        {
            bool isRet = false;
            const int nMaxReadSize = 65535;
            byte[] pBuffer = new byte[nMaxReadSize];
            FileStream m_pReadFile = new FileStream(strPath, FileMode.Open);
            do
            {
                if (!File.Exists(strPath))
                {
                    break;
                }
                if (pBuffer == null || pBuffer.Length <= 0)
                {
                    break;
                }
                if (m_pReadFile == null)
                {
                    break;
                }
                if (isBinary)
                {
                    BinaryReader pRead = new BinaryReader(m_pReadFile);
                    if (pRead == null)
                    {
                        break;
                    }
                    int nReadCount = 0;
                    while (nReadCount < m_pReadFile.Length)
                    {
                        pRead.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
                        Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
                        nReadCount += (int)pBuffer.Length;
                    }
                }
                else
                {
                    int nReadCount = 0;
                    while (nReadCount < m_pReadFile.Length)
                    {
                        m_pReadFile.Read(pBuffer, nReadCount, nMaxReadSize > (int)(m_pReadFile.Length - nReadCount) ? (int)(m_pReadFile.Length - nReadCount) : nMaxReadSize);
                        Console.WriteLine("txt:{0}", System.Text.Encoding.UTF8.GetString(pBuffer));
                        nReadCount += (int)pBuffer.Length;
                    }
                }
                isRet = true;
            } while (false);
            return isRet;
        }




    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一段老师写的,C#文件读取代码例子 的相关文章

  • R语言与面向对象的编程(3):R6类

    专注系列化 高质量的R语言教程 本号已支持快捷转载 无需白名单即可转载 本系列将介绍R语言中三个与面向对象的编程 Object Oriented Programming OOP 相关的工具包 proto R6和基础包methods 这是一个
  • python中,@和-> 代表什么?

    今天把代码放到Hadoop平台时调试代码的时候报错 但是在本地测试并没有什么问题 然后可查看了下代码 报错的地方这么定义的 看到这个符号觉得很奇怪 因为在Python中确实没见过这个符号 后来查了一下 参考这个博主写的 https blog
  • noip2008 火柴棒等式 (暴力枚举)

    P1496火柴棒等式 Accepted 标签 搜索 NOIP提高组2008 描述 给你n根火柴棍 你可以拼出多少个形如 A B C 的等式 等式中的A B C是用火柴棍拼出的整数 若该数非零 则最高位不能是0 用火柴棍拼数字0 9的拼法如图
  • 算法基础\BFS\DFS

    1 200 岛屿数量 题目描述 给你一个由 1 陆地 和 0 水 组成的的二维网格 请你计算网格中岛屿的数量 岛屿总是被水包围 并且每座岛屿只能由水平方向和 或竖直方向上相邻的陆地连接形成 此外 你可以假设该网格的四条边均被水包围 示例 示
  • 41.cuBLAS开发指南中文版--cuBLAS中的Level-2gemvBatched()

    2 6 24 cublas
  • numpy 三维矩阵下采样小技巧

    问题描述 我们有一个 160 192 224 的三维矩阵 由于其过大 跑神经网络时显存不够 此时我们的一个思路就是对其进行一个简单的下采样 即没3x3x3的小方格里只取其中间的那个数 现在的问题是如何快速高效地实现这个计算 而不是使用多个f

随机推荐

  • 爬虫代理IP池怎么来的,可能遇到哪些问题,怎么解决

    目录 前言 一 代理IP对爬虫工作的重要性 二 代理IP池从哪里来 三 爬虫工作中可能会遇到哪些问题 四 怎么解决遇到的问题 总结 前言 爬虫工作离不开代理IP的支持 代理IP在爬虫工作中发挥重要的作用 但爬虫代理IP池从哪里来呢 爬虫工作
  • 口令破解(概述、暴力破解、字典破解、Hydra)

    文章目录 口令破解 概述 口令安全现状 破解方式 暴力破解 字典破解 Hydra quarkspwdump 口令破解 概述 现在很多地方都以用户名 账号 和口令 密码 作为鉴权的方式 口令 密码 就意味着访问权限 口令 密码 就相当于进入家
  • sata接口_无线网卡M.2(ngff) keyA/E接口扩展sata硬盘接口,黑群辉NAS系统

    miniPC普及 主板小型化后 PCIE接口数量减少到1 2个 甚至没有 sata接口数量也大幅减少 这样的主板对于想使用多硬盘 特别是要黑群辉NAS系统的 是不适合的 好在某些主板有无线网卡M 2 ngff 接口 为扩展sata存留了希望
  • 华为手机助手上架流程_2019年各大安卓应用商店上架经验,含流程,物料,方法,建议收藏...

    注册应用商店账号 申请应用商店上架是APP推广的第一步 这一步没做好 会延迟甚至耽误后续的工作 因此 做好上架工作尤为重要 今天姑婆根据我们自己APP上架的经验进行了整理 分享给大家 希望对大家有所帮助 一 安卓应用商店格局 据调查显示在中
  • 【ubuntu】安装tensorRT

    tensorRT官方安装文档TensorRT3 Installation Guide RC pdf位于tensorRT下载页面 1 tensorRT下载 将tensorRT下载到想要安装的目录 https developer nvidia
  • Java字符串

    文章目录 Java字符串 一 Java常用 API 二 String 类 1 String 类的特点 1 Java 程序中所有双引号字符串 都是 String 类的对象 2 字符串在创建之后 其内容不可更改 3 字符串虽然不可改变 但是可以
  • 在vite+vue3项目下el-image引用本地图片失败

    地址使用 xxx xxxx 绝对路径 地址使用require xx xx 和require xx xx 地址使用相对路径不行 xxx xxx
  • Unity 动态鼠标切换

    Header 获得需要替换的鼠标纹理 SerializeField private Texture2D cursorTexture private void OnMouseEnter 鼠标进入更换鼠标纹理 Cursor SetCursor
  • SpringBoot默认的日志管理logback

    1 SpringBoot使用的默认日志框架是Logback 并用INFO级别输出到控制台 日志输出内容元素具体如下 时间日期 精确到毫秒 日志级别 ERROR WARN INFO DEBUG TRACE 进程ID 分隔符 标识实际日志的开始
  • 为什么说声明式API比命令式API更优雅?

    点击上方 分布式实验室 关注公众号 回复 1 抽取纸质技术书 越来越多的工具已经从命令式范式转变为声明式范式 在本文中 我提出了一个框架 以帮你系统理解 React Kubernetes Terraform 等工具的架构 这些工具将有状态逻
  • Python线程池

    Python可以配置线程池 线程池的作用 预先开启设定的线程 当一个线程结束以后可以让程序继续使用该线程 设置线程的最大数目 让系统不至于因为开启多个线程而崩溃 在有大量空闲时间的进程中 配置多线程可以让程序并行处理 提高处理速度 线程池的
  • Golang基础教程

    第一章 走进Golang Golang引入 简介 Go 又称 Golang 是 Google 的 Robert Griesemer Rob Pike 及 Ken Thompson 开发的一种计算机编程语言语言 设计初衷 Go语言是谷歌推出的
  • MacOS配置Sql Server环境

    看了网上大多都是介绍Windows系统的Sql Server配置 对于Mac用户来说 这是有些不太友好的 找了好久都没有详细的解答 故现在对此做一个教程 方便日后回顾 当然 如果对于Windows上配置有任何疑问也可以留言 下面则是教程 首
  • 如何在虚拟机Ubuntu下使用主机网络

    目录 配置 主要参考链接 详细步骤 第零步 确保主机网络可共享 第一步 第二步 第三步 第四步 配置 Win11 VMware Ubuntu 18 04 主要参考链接 Ubuntu虚拟机共享主机VPN 适用于NAT或桥接 百度文库 baid
  • 常用DateUtil

    导语 最近项目中用到一些时间的操作 当然我们可以选择lang3 或者其他三方的jar 来进行操作 小编将项目中用到的一些用于的时间工具整理了一下 给大家参考一下 先给大家展示一下使用及效果 使用 GetMapping value getTi
  • 现在才知道掌握IDEA、VS Code这些常用快捷键有多方便

    IDEA VS Code这些常用快捷键 文章目录 IDEA VS Code这些常用快捷键 前言 一 IDEA快捷键 1 控制台语句 System out 相关 2 查找 3 跳转切换 4 编码相关 public static void ma
  • sqlmap --os-shell 使用方法

    一 burp suite抓包 如上图所示 红框处很明显是一个传参点 我们就在这个页面抓包 抓到包之后将内容保存到桌面的1000 txt文件下 二 sqlmap跑包 打开sqlmap跑包 python sqlmap py r C Users
  • QT 信号和槽的四种连接方式

    QT 信号和槽的四种连接方式 一 右键点击UI中的控件 转到槽 二 使用宏 三 使用引用 四 使用Lambda表达式 假设使用场景为点击PushButton按钮 响应槽函数 mySlot 一 右键点击UI中的控件 转到槽 系统会自动在Mai
  • java抽象类实现接口可以不用实现方法

    java抽象类实现接口可以不用实现方法 学习struts2时 拦截器接口Interceptor继承了Serializable接口 拦截器抽象类AbstractInterceptor实现了Interceptor接口 在AbstractInte
  • 一段老师写的,C#文件读取代码例子

    using System using System IO using System Collections namespace FileOperation public class TestFile public static void M