C#__基本的读写文件方式

// 代码+注释

cs 复制代码
    class Program
    {
        /// <summary>
        /// Path类:
        /// 不能实例化;提供了一些静态方法,更容易对路径名执行操作;(有兴趣可以自行了解)
        /// 
        /// 读写文件:File类
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // ReadAllText(): 打开一个文本文件,读取文件的所有行,然后关闭该文件
            Console.WriteLine(File.ReadAllText(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt"));
            /*
            这是一个文本文件
            小米南瓜是一种美味的食品
            欢迎大家前来品尝
            */

            // 打开一个文本文件,读取文件的所有行,然后关闭该文件
            string[] strArray = File.ReadAllLines(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt");
            foreach(string str in strArray)
            {
                Console.Write(str);
            }
            Console.WriteLine();
            // 这是一个文本文件小米南瓜是一种美味的食品欢迎大家前来品尝

            // 打开一个二进制文件(将文件转换为二进制文件),将文件的内容读入一个字节数组(0~255),然后关闭该文件
            Byte[] byteArray = File.ReadAllBytes(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt");
            int i = 0;
            while (i < byteArray.Length)
            {
                if (15 == i % 16)
                {
                    Console.WriteLine(byteArray[i]);
                }
                else
                {
                    Console.Write(byteArray[i] + " ");
                }
                i++;
            }
            /*
            239 187 191 232 191 153 230 152 175 228 184 128 228 184 170 230
            150 135 230 156 172 230 150 135 228 187 182 13 10 229 176 143
            231 177 179 229 141 151 231 147 156 230 152 175 228 184 128 231
            167 141 231 190 142 229 145 179 231 154 132 233 163 159 229 147
            129 13 10 230 172 162 232 191 142 229 164 167 229 174 182 229
            137 141 230 157 165 229 147 129 229 176 157
            */

            // 创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则覆盖该文件。
            File.WriteAllText(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt","Hello World!");
            Console.WriteLine(File.ReadAllText(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt"));
            // Hello World!

            // 创建一个新文件,在其中写入指定的字节数组,然后关闭该文件(注意:每打印一行会换行,实际行数为输入的数组+1)
            File.WriteAllLines(@"D:\C#编程\文件操作之FileInfo和DirectoryInfo\TextFile1.txt", new string[]{ "1","2","3"});
            /*
             * 1
             * 2
             * 3
             
             */

            // Read:在内存中读文件,一般情况下不使用(不推荐)
        }
    }
相关推荐
Yeauty7 分钟前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu38 分钟前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克1 小时前
【C++】set 与multiset
开发语言·c++
Wang's Blog1 小时前
Go-Zero项目开发34: 微服务超时控制与重试机制实践
开发语言·微服务·golang·go-zero
2zcode2 小时前
项目文档:基于MATLAB神经网络的心力衰竭预测与临床辅助决策系统研究
开发语言·神经网络·matlab·心力衰竭预测\
减瓦3 小时前
深入 Quarkus:云原生时代 Java 的重生之路
java·开发语言·云原生
杨运交3 小时前
[053][核心模块]Java枚举缓存与ORM集成实践
java·开发语言·缓存
2401_894915534 小时前
Geo优化系统源码部署搭建技术——PHP程序开发部署指南
开发语言·php
wuyk5554 小时前
67.嵌入式C语言进阶:结构体指针实战指南——STM32外设、传感器数据访问的高效技巧
c语言·开发语言·stm32·单片机
找磁材4 小时前
空心杯电机与永磁体
开发语言·磁性材料·钕铁硼·磁钢