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的文章供大家温习。希望对大家有帮助。
    绿色通道: 好文要顶 关注我 收藏该文与我联系
  • 相关阅读:
    BZOJ3578:GTY的人类基因组计划2(集合hash,STL)
    【BZOJ 1022】 [SHOI2008]小约翰的游戏John
    【BZOJ 1295】 [SCOI2009]最长距离
    【BZOJ 1103】 [POI2007]大都市meg
    【BZOJ 3172】 [Tjoi2013]单词
    【BZOJ 1067】 [SCOI2007]降雨量
    【BZOJ 1491】 [NOI2007]社交网络
    【BZOJ 1087】[SCOI2005]互不侵犯King
    【BZOJ 1009】 [HNOI2008]GT考试
    【BZOJ 1053】[HAOI2007]反素数ant
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/3559618.html
Copyright © 2011-2022 走看看