File(檔案)對象代表—個檔案,並為檢測檔案是否存在、檔案更名和檔案刪除提供了一些有用的方法。所有這些方法都是靜態,也就是說,不能使用NEW運算子建立File的執行個體,但可以直接使用File的方法。
if(File.Existas("File.txt"))
File.Delete("File.txt");
也可以用File對象獲得用於讀寫檔案資料的檔案流(FileStream)。
FileStream fs = File.OpenRead("File.any");
FileStream fs = File.OpenText("File.txt");
下面是一些有用的File方法
------------------------------------------------
File.FileExists(fileName)
File.Delete(fileName)
File.AppendText(String)
File.Copy(fromFile,toFile)
File.Move(fromFile,toFile)
File.GetExtension(fileName) //擷取檔案的副檔名
File.HasExtension(fileName) //若檔案有副檔名,則返回true
-----------------------------------------------------------
要讀一個文字檔,首先使用File對象擷取一個StreamReader對象,然後用文字資料流的話方法讀取資料。
StreamReader ts = File.OpenText("File.txt");
string s = ts.ReadLine();
要建立一個文字檔並寫入資料,可使用CreateText方法擷取—個StreamWrite對象。
//open for writing
StreamWriter sw = File.CreateText("File.txt");
sw.WriteLine("Hello file");
如果向—個已經存在的檔案追加資料,可以直接建立—個StreamWrite對象,並將用於追加的那個布爾參數設定為true。
//append to text file
StreamWriter sw = new StreamWriter("file.txt",true);
=====================================================
大量經常發生的異常是在處理檔案的輸入和輸出時引起的。非法檔案名稱、檔案不存在、目錄不存在、非法檔案名稱參數及檔案保護錯誤等都會引起異常。因此,處理檔案輸入和輸出的最好方法是
將檔案處理代碼放在try塊中,這樣可以保證擄獲所有可能的錯誤,避免出現令人為難的致命錯誤。
各種檔案類的方法都在其文檔中結出了它們能拋出的異常。只要捕獲通用Ex配必叨對象就能保證
捕獲所有異常,但是如果需要對不同的異常進行不同的處理,既要對異常分別進行檢測。
例如,可以按照下列方式開啟一個文字檔。
try
{
StreamReader ts = File.OpenText("File.txt");
string s = ts.ReadLine();
}
catch(Exception e )
{
console.writeLine(e.Message);
}
檢測檔案末尾
有兩種可用的方法能保證不會超過文字檔的末尾:(1)尋找空(null)異常,(2)尋找資料
流的末尾。當讀資料超過了文字檔的末尾時,不會出現錯誤,也不會拋出檔案結束的異常。但是,如果在檔案末尾後讀人一個字元中,會返回一個空值,可以用它在一個檔案讀人類中建立一個檔案結束函數。
private StreamReader rf;
private bool eof;
//-----------------------------------
Public String readLine()
{
string s = rf.ReadLine();
if(s==null)
eof=true;
return s;
}
//-----------------------------------
public bool fEof()
{
return eof;
}
另外一種保證讀資料不會超過檔案末尾的方法是,使用Stream的Peek方法在讀資料前先查看一下。Peek方法返迴文件中下一個字元的ASCII碼,如果沒有字元則返回 -1.
public string read_line()
{
string s="";
if(rf.Peek()>0)
s = rf.ReadLine();
else
eof=true;
return s;
}
===================================
csFile.cs 類
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace OperateFile
{
public class csFile
{
private string fileName;
StreamReader ts;
StreamWriter ws;
private bool opened, writeOpened;
public csFile()
{
init();
}
public csFile(string file_name)
{
fileName = file_name;
init();
}
private void init()
{
opened = false;
writeOpened = false;
}
//開啟欲讀資料的檔案
public bool OpenForRead()
{
return OpenForRead(fileName);
}
//開啟欲讀資料的檔案
public bool OpenForRead(string file_name)
{
fileName = file_name;
try
{
ts = new StreamReader(fileName);
opened = true;
}
catch(FileNotFoundException e)
{
return false;
}
return true;
}
//從檔案讀取資料
public string readLine()
{
return ts.ReadLine();
}
//寫檔案
public void writeLine(string s)
{
ws.WriteLine(s);
}
public bool OpenForWrite()
{
return OpenForWrite(fileName);
}
public bool OpenForWrite(string file_name)
{
try
{
ws = new StreamWriter(file_name);
fileName = file_name;
writeOpened = true;
return true;
}
catch(FileNotFoundException e)
{
return false;
}
}
}
}