zoukankan      html  css  js  c++  java
  • C#操作文件

    再编程的过程中操作文件是我们经常可以遇到的,本文用来写些操作文件的简单实例。

    C#操作二进制文件

    直接上代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    using System;
    using System.IO;
     
    namespace TestFile
    {
        class Program
        {
            //操作二进制文件简单demo
            private static string streamFile = "stream.data";
            static void Main(string[] args)
            {
                Console.WriteLine("开始写文件");
                using (BinaryWriter writer = new BinaryWriter(File.Open(streamFile, FileMode.Create)))
                {
                    writer.Write("hello world!");
                    Console.WriteLine("文件写成功");
                }
                using (BinaryReader br = new BinaryReader(File.Open(streamFile, FileMode.Open)))
                {
                    Console.Write("文件读出的内容是:");
                    Console.Write(br.ReadString()+" ");
                }
                Console.ReadKey();
            }
        }
    }
    运行结果: 二进制  

    C#操作文本文件

    直接上实例代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    using System;
    using System.IO;
     
    namespace TestFile
    {
        class Program
        {
            //操作文本文件简单demo
            private static string filePath = @"F:file.txt";
            static void Main(string[] args)
            {
                Console.WriteLine("写数据测试开始");
     
                //写数据);
                if (File.Exists(filePath))//注意using的用法
                {
                    using (StreamWriter sw = File.CreateText(filePath))
                    {
                        sw.Write("hello world! my file test");
                        Console.WriteLine("写入数据成功");
                    }
                }
                Console.WriteLine("读数据测试开始");
                //读取文件数据);
                if (File.Exists(filePath))
                {
                    using (StreamReader sr = File.OpenText(filePath))
                    {
                        string strRead = string.Empty;
                        while ((strRead = sr.ReadLine()) != null)
                        {
                            Console.WriteLine(strRead);
                        }
                    }
                }
                try
                {
                    File.Copy(filePath, @"F:file1.txt");
                    File.Delete(filePath);
                    Console.WriteLine("删除文件成功");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("删除文件失败:" + ex.Message);
                }
                Console.ReadKey();
            }
        }
    }
    运行结果: 文本文件  

    总结

    本文介绍了常用的两种文件的读写,这是最基本的操作方式。一般情况下我们都会把上面的一些操作封装成工具类,从而进行文件的读写。当然xml文件也是文本文件的一种,想了解如何封装的请参考:wince平台用xml文件做配置文件。 还有一点需要注意的是本文实例代码中using的使用,我会在温习系列中写一篇有关using的文章供大家温习。希望对大家有帮助。
    绿色通道: 好文要顶 关注我 收藏该文与我联系
  • 相关阅读:
    Python 命令模式和交互模式
    Python自带IDE设置字体
    Python2.7和3.7区别
    Kubernetes1.91(K8s)安装部署过程(八)-- kubernetes-dashboard安装
    Kubernetes1.91(K8s)安装部署过程(七)--coredns安装
    nginx 设置自签名证书以及设置网址http强制转https访问
    Kubernetes1.91(K8s)安装部署过程(六)--node节点部署
    VMware安装VMware tool是 遇到The path "" is not a valid path to the 3.10.0-693.el7.x86_64 kernel headers.
    第三方git pull免密码更新
    Kubernetes1.91(K8s)安装部署过程(五)--安装flannel网络插件
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/3559618.html
Copyright © 2011-2022 走看看