• C#读写txt文件


    View Code
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.IO;
      6 
      7 namespace Files
      8 {
      9     class Program
     10     {
     11         static byte[] byDataValue = new byte[200];
     12         static char[] charDataValue = new char[200];
     13         static void Main(string[] args)
     14         {
     15             FileStreamTest();
     16             StreamWriteFile();
     17             StreamReaderTestTwo();
     18             Console.ReadKey();
     19         }
     20 
     21         /// <summary>
     22         /// 读文件
     23         /// </summary>
     24         private static void FileStreamTest() 
     25         {           
     26             try
     27             {
     28                 FileStream fs = new FileStream(@"f:\test.txt", FileMode.Open);
     29                 //fs.Seek(20, SeekOrigin.Begin);
     30                 fs.Read(byDataValue, 0, 200);
     31                 fs.Close();
     32             }
     33             catch (Exception)
     34             {                
     35                 throw;
     36             }
     37             //将字节转换成字符
     38             Decoder dc = Encoding.UTF8.GetDecoder();
     39             //字节数组转换成字符数组
     40             dc.GetChars(byDataValue,0,byDataValue.Length,charDataValue,0);
     41 
     42             foreach (char c in charDataValue)
     43             {
     44                 Console.Write(c);
     45             }           
     46         }
     47 
     48         /// <summary>
     49         /// 写文件
     50         /// </summary>
     51         private static void StreamWriteFile()
     52         {
     53             try
     54             {
     55                 FileStream fs = new FileStream(@"f:\test.txt", FileMode.Create);
     56                 StreamWriter sw = new StreamWriter(fs);
     57                 sw.WriteLine("hello world , this is c sharp in dot net");
     58                 sw.Close();
     59             }
     60             catch (Exception)
     61             {                
     62                 throw;
     63             }        
     64         }
     65 
     66         /// <summary>
     67         /// 读取文件1
     68         /// </summary>
     69         private static void StreamReaderTest()
     70         {
     71             string sLine = "";
     72             try
     73             {
     74                 FileStream fsFile = new FileStream(@"f:\test.txt", FileMode.Open);
     75                 StreamReader srReader = new StreamReader(fsFile);
     76                 //读取文件(读取大文件时,最好不要用此方法)
     77                 sLine = srReader.ReadToEnd();              
     78                 srReader.Close();
     79                 Console.Write(sLine);
     80             }
     81             catch(Exception e)
     82             {
     83                 throw e;
     84             }
     85         }
     86 
     87         /// <summary>
     88         /// 读取文件2
     89         /// </summary>
     90         private static void StreamReaderTestTwo()
     91         {
     92             string sLine = "";
     93             try
     94             {
     95                 FileStream fsFile = new FileStream(@"f:\test.txt", FileMode.Open);
     96 
     97                 StreamReader srReader = new StreamReader(fsFile);
     98                 int iChar;
     99                 iChar = srReader.Read();
    100                 while (iChar != -1)
    101                 {
    102                     sLine += (Convert.ToChar(iChar));
    103                     iChar = srReader.Read();
    104                 }
    105                 srReader.Close();
    106                 Console.WriteLine(sLine);
    107             }
    108             catch (Exception e)
    109             {
    110                 throw e;
    111             }
    112         }
    113     }
    114 
    115     /*
    116     注:FileMode枚举成员值在文件存在与不存在时的情况 
    117 
    118     Append
    119      文件存在时:打开文件,流指向文件的末尾,只能与枚举FileAcess.Write联合使用
    120      文件不存在时:创建一个新文件。只能与枚举FileAcess.Write联合使用
    121  
    122     Create
    123      文件存在时:删除该文件,然后创建新文件
    124      文件不存在时:创建新文件
    125  
    126     CreateNew
    127      文件存在时:抛出异常
    128      文件不存在时:创建新文件
    129  
    130     Open
    131      文件存在时:打开现有文件,流指向文件开头
    132      文件不存在时:抛出异常
    133  
    134     OpenOrCreate
    135      文件存在时:打开文件,流指向文件开头
    136      文件不存在时:创建新文件
    137  
    138     Truhncate
    139      文件存在时:打开现有文件,清除其内容。流指向文件开头,保留文件的初始创建日期。
    140      文件不存在时:抛出异常
    141      * 
    142      */
    143 
    144 }
    工欲善其事,必先利其器。
  • 相关阅读:
    koa2 nginx 服务器配置
    Spring Cloud 中OpenFeign的使用(二)
    Spring Cloud中OpenFeign的使用(一)
    Spring Cloud Alibab Sentinel服务端搭建
    asp.net core 读取 appsettings.json 节点值
    c# – AuthenticationHeaderValue与NetworkCredential
    元气
    艾维利时间管理法
    BPM/OA/审批流/工作流
    消息队列
  • 原文地址:https://www.cnblogs.com/zhangzhu/p/2836164.html
Copyright © 2020-2023  润新知