//Using System.IO
System.io命名空間中常用的成員類別
BiaryReader
Binarywriter
Streamreader
streamwriter
stringReader
stringwriter
Directory
DircetoryInfo 沒有安全檢查
Driveinfo 哪些磁碟可用,類型,容量,可用空間
file
fileinfo
filestream
path
//BinaryReader
BinaryReader [BinaryReader 名稱] = new BinaryReader([Stream 名稱]);
BinaryReader 常用方法
close() 關閉binaryReader及資料流ex:
FileStream fs = new FileStream(@"c:/a.txt"FileMode.Open);
BinaryReader br = new BinaryReader(fs);
br.close();
PeekChar() 傳回下一個字元的accii碼(integer),但不移動指標,若傳回-1代表下一字元無資料
FileStream fs = new FileStream(@"c:/a.txt"FileMode.Open);
BinaryReader br = new BinaryReader(fs);
messagebox.show(br.peekchar().tostring());
read(char[],a,b)從a開始的b個字符讀到char[]中 ex:
FileStream fs = new FileStream(@"c:/a.txt"FileMode.Open);
BinaryReader br = new BinaryReader(fs);
char[] Array = new char[10];
br.read(array,3,7)
readboolean() 讀一字節布林值返回,並將指標移到下一個byte
readbyte() 以整數方式讀取1byte,並將指標移到下一個byte
readbyte(a) 以整數方式讀取a個byte,指標向後移動a表byte
readchar() 以字元方式讀取1byte,指標移到下一個byte
readchar(a) 字元方式讀取a個byte,指標向後移動a表byte
readdecimal() 以十進制方式讀取,指標向後移16個byte
readdouble() 以浮點的方式讀取8byte,指標向後移8 byte
readint16() 以整數方式讀2byte,指標向後移2byte
readint32() 以整數方式讀4byte,指標向後移4byte
readint64() 以整數方式讀8byte,指標向後移8byte
System.text.encodeing 包含的成員表
ASCII ASCII的編碼方式(7bits)
BigEndianUnicode UTF16格式的編碼方式,byte的順序為big-endian
default 取得目前系統的ANSII編碼方式
Unicode UTF-16格式的編碼方式,byte的順序為little-endig
utf32 UTF_32格式的編碼方式,byte的順序為little-endig
UTF7
UTF8
StreamReader 的屬性
CurrentEncodeing 取得目前streamreader的編碼方式。
endofstream 取得指標是否在資料尾
streamreader的常用方法
close
peek()
read(char[],a,b)
readline()
readtoend() ex: fliestream fs=new filesteam(@"c:/a.txt",filemode.open);
streamreader sr=new streamrader(fs,system.text.encoding.default);
messagebox.show(sr.readtoend());
//code ex:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Reader_Ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FileStream fs;
private void Form1_Load(object sender, EventArgs e)
{
fs = new FileStream(Application.StartupPath + @"/Test.txt", FileMode.Open);
txt_Binary.Text = "";
}
private void btn_Binary_Click(object sender, EventArgs e)
{
BinaryReader br = new BinaryReader(fs);
if (comboBox_Binary.SelectedIndex == -1)
{
//使用者未選擇讀取方式。
}
else
{
try
{
if (br.PeekChar() != -1)
{
switch (comboBox_Binary.SelectedItem.ToString())
{
case "PeekChar()-傳回下一個字元的ASCII碼(Integer)":
txt_Binary.Text = txt_Binary.Text + br.PeekChar().ToString();
break;
case "ReadByte()-以整數的方式讀取1 byte":
txt_Binary.Text = txt_Binary.Text + br.ReadByte().ToString();
break;
case "ReadChar()-以字元的方式讀取1 byte":
txt_Binary.Text = txt_Binary.Text + br.ReadChar().ToString();
break;
case "ReadDecimal()-以十進位的方式讀取並傳回十進位值":
txt_Binary.Text = txt_Binary.Text + br.ReadDecimal().ToString();
break;
case "ReadInt64()-以整數的方式讀取8 bytes":
txt_Binary.Text = txt_Binary.Text + br.ReadInt64().ToString();
break;
}
}
else
{
MessageBox.Show("已至檔案結尾");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
}
private void btn_Stream_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
if (comboBox_Stream.SelectedIndex == -1)
{
//使用者未選擇讀取方式。
}
else
{
try
{
if (sr.Peek() != -1)
{
switch (comboBox_Stream.SelectedItem.ToString())
{
case "Peek()-傳回下一個字元":
txt_Stream.Text = txt_Stream.Text + sr.Peek().ToString();
break;
case "ReadLine()-讀取資料中的一行":
txt_Stream.Text = txt_Stream.Text + sr.ReadLine().ToString();
break;
case "ReadToEnd()-讀取資料流中所有資料":
txt_Stream.Text = txt_Stream.Text + sr.ReadToEnd().ToString();
break;
}
}
else
{
MessageBox.Show("已至檔案結尾");
}
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
}
}
}
BinaryWrite 常用方法
close() 關閉binaryWriter與
seek() 設定目前binaryWriter資料流的指標位置
write(s) 將s字串字入資料流
write(char[],a,b) 將字元陣列寫入資料流,char[]從a位置寫入b個