欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#使用FileStream對(duì)象讀寫文件

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

在項(xiàng)目開發(fā)中經(jīng)常會(huì)涉及到對(duì)文件的讀寫,c# 提供了很多種方式來對(duì)文件進(jìn)行讀寫操作,今天來說說FileStream 對(duì)象。

FileStream表示在磁盤或網(wǎng)絡(luò)路徑上指向文件的流。一般操作文件都習(xí)慣使用StreamReader 和 StreamWriter,因?yàn)樗鼈儾僮鞯氖亲址麛?shù)據(jù) 。而FileStream 對(duì)象操作的是字節(jié)和字節(jié)數(shù)組。有些操作是必須使用FileStream 對(duì)象執(zhí)行的,如隨機(jī)訪問文件中間某點(diǎn)的數(shù)據(jù)。

創(chuàng)建FileStream 對(duì)象有許多不同的方法,這里使用文件名和FileMode枚舉值創(chuàng)建:

一、 讀取文件,記得引用 System.IO 命名空間:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplicationTest
{
 class Program
 {
  static void Main(string[] args)
  {
   //創(chuàng)建需要讀取的數(shù)據(jù)的字節(jié)數(shù)組和字符數(shù)組
   byte[] byteData = new byte[200];
   char[] charData = new char[200];

   //捕獲異常:操作文件時(shí)容易出現(xiàn)異常,最好加上try catch
   FileStream file = null;
   try
   {
    //打開一個(gè)當(dāng)前 Program.cs 文件,此時(shí)讀寫文件的指針(或者說操作的光標(biāo))指向文件開頭
    file = new FileStream(@"..\..\Program.cs", FileMode.Open);
    //讀寫指針從開頭往后移動(dòng)10個(gè)字節(jié)
    file.Seek(10, SeekOrigin.Begin);
    //從當(dāng)前讀寫指針的位置往后讀取200個(gè)字節(jié)的數(shù)據(jù)到字節(jié)數(shù)組中
    file.Read(byteData, 0, 200);
   }catch(Exception e)
   {
    Console.WriteLine("讀取文件異常:{0}",e);
   }
   finally
   {
    //關(guān)閉文件流
    if(file !=null) file.Close();
   }
   //創(chuàng)建一個(gè)編碼轉(zhuǎn)換器 解碼器
   Decoder decoder = Encoding.UTF8.GetDecoder();
   //將字節(jié)數(shù)組轉(zhuǎn)換為字符數(shù)組
   decoder.GetChars(byteData, 0, 200, charData, 0);
   Console.WriteLine(charData);
   Console.ReadKey();
  }
  }
}

顯示結(jié)果如下:

二、寫入文件:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplicationTest
{
 class Program
 {
  static void Main(string[] args)
  {
   byte[] byteData;
   char[] charData;
   FileStream file = null;
   try
   {
    //在當(dāng)前啟動(dòng)目錄下的創(chuàng)建 aa.txt 文件
    file = new FileStream("aa.txt", FileMode.Create);
    //將“test write text to file”轉(zhuǎn)換為字符數(shù)組并放入到 charData 中
    charData = "Test write text to file".ToCharArray();
    byteData = new byte[charData.Length];
    //創(chuàng)建一個(gè)編碼器,將字符轉(zhuǎn)換為字節(jié)
    Encoder encoder = Encoding.UTF8.GetEncoder();
    encoder.GetBytes(charData, 0, charData.Length, byteData, 0,true);
    file.Seek(0, SeekOrigin.Begin);
    //寫入數(shù)據(jù)到文件中
    file.Write(byteData, 0, byteData.Length);

   }catch(Exception e)
   {
    Console.WriteLine("寫入文件異常:{0}",e);
   }
   finally
   {
    if (file != null) file.Close();
   }
   
   Console.ReadKey();
  }
 }
}

結(jié)果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C#實(shí)現(xiàn)時(shí)間戳的簡(jiǎn)單方法

欄    目:C#教程

下一篇:c#實(shí)現(xiàn)最簡(jiǎn)潔的快速排序(你絕對(duì)可以看懂)

本文標(biāo)題:C#使用FileStream對(duì)象讀寫文件

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4773.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有