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的文章供大家温习。希望对大家有帮助。
    绿色通道: 好文要顶 关注我 收藏该文与我联系
  • 相关阅读:
    index of rmvb mp3 rm突破站点入口下载
    人类智商一般在多少左右?爱因斯坦的智商是多少?
    UVALive 5102 Fermat Point in Quadrangle 极角排序+找距离二维坐标4个点近期的点
    ProgressDialog使用总结
    Js中的多条件排序,多列排序
    腾讯2014年实习生招聘笔试面试经历
    周根项《一分钟速算》全集播放&下载地址
    中国大推力矢量发动机WS15 跨入 世界先进水平!
    探索Android中的Parcel机制(上)
    ORACLE uuid自己主动生成主键
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/3559618.html
Copyright © 2011-2022 走看看