文件目錄
相關(guān)類
- DirectoryInfo
- Direcoty
- FileInfo
- File
相關(guān)操作API:
- Create
- Exists
- MoveTo
- Open
文件相關(guān)屬性
- Name
- FullName
- CreationTime
- Attributes
流 IO
定義
流代表在源文件和目標(biāo)文件之間傳輸一定量的數(shù)據(jù)
通過流訪問網(wǎng)絡(luò),內(nèi)存地址,和其它與流相關(guān)的設(shè)備
常用流類
FileStream 讀取和寫入一個(gè)字節(jié)或者字節(jié)數(shù)組
static void TestFileStream()
{
using (FileStream fs = File.Open("msg.dat", FileMode.Create))
{
string msg = "hello";
byte[] msgByteArray = Encoding.Default.GetBytes(msg);
fs.Write(msgByteArray, 0, msgByteArray.Length);
fs.Position = 0;
byte[] byteFromFile = new byte[msgByteArray.Length];
for(int i = 0; i < msgByteArray.Length; i++)
{
byteFromFile[i] = (byte)fs.ReadByte();
Console.Write(byteFromFile[i]);
}
}
}
StreamWriter 基于字符的讀寫流
static void TestStreamWriter()
{
using (StreamWriter writer = File.CreateText("test.txt"))
{
writer.WriteLine("hello");
writer.WriteLine("world");
writer.WriteLine("its a test file create by streamwriter");
}
}
/// <summary>
/// 將文本信息當(dāng)作內(nèi)存中的字符一樣處理
/// </summary>
static void TestStreamReader()
{
using (StreamReader reader = File.OpenText("test.txt"))
{
string input = null;
while((input == reader.ReadLine()))
{
Console.WriteLine(input);
}
}
}
StreamReader 將文本信息當(dāng)作內(nèi)存中的字符一樣處理
static void TestStreamReader()
{
using (StreamReader reader = File.OpenText("test.txt"))
{
string input = null;
while((input == reader.ReadLine()))
{
Console.WriteLine(input);
}
}
}
static void TestStringWriter()
{
using (StringWriter writer = new StringWriter())
{
writer.WriteLine("hahhhhh ");
writer.WriteLine("aaaaaa ");
Console.WriteLine(writer);
Console.WriteLine("----------");
using (StringReader reader = new StringReader(writer.ToString()))
{
string input = null;
while((input = reader.ReadLine()) != null)
{
Console.WriteLine(input);
}
}
}
}
BinaryWriter 二進(jìn)制格式讀寫數(shù)據(jù)流窗轩,支持隨機(jī)數(shù)據(jù)訪問 Seek
static void TestBinaryWriter()
{
FileInfo f = new FileInfo("BinFile.dat");
using(BinaryWriter bw = new BinaryWriter(f.OpenWrite()))
{
Console.WriteLine(bw.BaseStream);
double aDouble = 12233.22;
int anInt = 1022;
string aString = "a,b,c";
bw.Write(aDouble);
bw.Write(anInt);
bw.Write(aString);
}
Console.WriteLine("Done!");
}
static void TestBinaryReader()
{
FileInfo f = new FileInfo("BinFile.dat");
using (BinaryReader br = new BinaryReader(f.OpenRead()))
{
Console.WriteLine(br.ReadDouble());
Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadString());
}
}
觀察文件變動(dòng) FileSystemWatcher
static void TestFileWatcher()
{
FileSystemWatcher watcher = new FileSystemWatcher();
try
{
watcher.Path = ".";
}
catch(ArgumentException ex)
{
Console.WriteLine(ex.Message);
return;
}
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.DirectoryName
| NotifyFilters.FileName;
//只觀察文本文件
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
//開始觀察目錄
watcher.EnableRaisingEvents = true;
Console.WriteLine(@"Press 'q' to quit app.");
while (Console.Read() != 'q') ;
}
static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File:{0} {1}", e.FullPath, e.ChangeType);
}
static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
序列化
定義
持久化一個(gè)對象的狀態(tài)到流(文件流和內(nèi)存流)的過程。
BinaryFormatter 二進(jìn)制序列化
static void TestSerialize()
{
UserPref userData = new UserPref();
userData.WindowColor = "Red";
userData.FontSize = 50;
BinaryFormatter binFormat = new BinaryFormatter();
using(Stream fStream = new FileStream("user.dat", FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, userData);
}
}
static void LoadFromBinaryFile()
{
BinaryFormatter binFormat = new BinaryFormatter();
using (Stream fStream = File.OpenRead("user.dat"))
{
UserPref userData = (UserPref)binFormat.Deserialize(fStream);
Console.WriteLine("UserPref {0},{1}", userData.WindowColor, userData.FontSize);
}
}
[Serializable]
public class UserPref
{
public string WindowColor;
public int FontSize;
}
//參與序列化缘滥,不可繼承
[Serializable]
public class Radio
{
public bool hasTweeners;
public bool hasSubWoofers;
public double[] stationPresets;
//不參與序列化
[NonSerialized]
public string radioID = "XF-3344";
}