C#基礎筆記(第二十一天)

來源:互聯網
上載者:User

標籤:dispose   end   應用   方式   default   adk   兩種   筆記   des   

1.FIle類、Path類、Directory類複習
操作檔案的
File 操作檔案,靜態類,對檔案整體操作。拷貝、刪除、剪下等。
Directory 操作目錄(檔案夾),靜態類。
Path 對檔案或目錄的路徑進行操作(很方便)[字串]
Strean 檔案流,抽象類別。

Path 操作檔案路徑
File 操作檔案
複製、剪下、建立、移除
//File.Create(@"C:\Users\SJD\Desktop\new.txt");
//Console.WriteLine("建立成功");
//Console.ReadKey();

//File.Delete(@"C:\Users\SJD\Desktop\new.txt");
//Console.WriteLine("刪除成功");
//Console.ReadKey();

//File.Move(@"C:\Users\SJD\Desktop\new.txt", @"C:\Users\SJD\Desktop\1.txt");
//Console.WriteLine("剪下成功");
//Console.ReadKey();

使用File類來讀取資料(讀取小檔案,因為是一次性讀取,大檔案用檔案流來讀取)

File的三個讀取的方法

1. 以位元組數組的形式讀取
byte[] buffer = File.ReadAllBytes(@"C:\Users\SJD\Desktop\123.txt");
string str = Encoding.Default.GetString(buffer, 0, buffer.Length);
Console.WriteLine(str);
Console.ReadKey();

2.以字串數組的形式讀取,用在操作每行資料上
string[]str= File.ReadAllLines(@"C:\Users\SJD\Desktop\123.txt",Encoding.Default);
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
Console.ReadKey();

3.以字串的形式讀取,只用於看一下,不做任何操作
string str = File.ReadAllText(@"C:\Users\SJD\Desktop\123.txt", Encoding.Default);
Console.WriteLine(str);
Console.ReadKey();

File的三個寫入的方法

1. 以位元組數組的形式寫入,會覆蓋原來的內容
string str = "今天天氣好晴朗,處處好風光";
byte[] buffer = Encoding.Default.GetBytes(str);
File.WriteAllBytes(@"C:\Users\SJD\Desktop\1.txt", buffer);
Console.WriteLine("寫入成功");
Console.ReadKey();

2.以字串數組的形式寫入一行一行
File.WriteAllLines(@"C:\Users\SJD\Desktop\1.txt", new string[] { "今天天氣好晴朗,處處好風光" });
Console.WriteLine("寫入成功");
Console.ReadKey();

3.以字串的形式直接寫入
string str = "今天天氣好晴朗,處處好風光";
File.WriteAllText(@"C:\Users\SJD\Desktop\1.txt", str);
Console.WriteLine("寫入成功");
Console.ReadKey();

4.追加不覆蓋,前面加上Append,有AppendAllLines和AppendAllText兩種
File.AppendAllText(@"C:\Users\SJD\Desktop\1.txt", "肯定沒有覆蓋");
Console.WriteLine("追加成功");
Console.ReadKey();

Directory類
1.建立指定路徑的檔案夾
Directory.CreateDirectory(@"C:\Users\SJD\Desktop\建立檔案夾");
Console.WriteLine("建立成功");
Console.ReadKey();

2.刪除指定路徑的檔案夾,檔案夾目錄不是空的就不能刪,非要刪除,後面加個true
Directory.Delete(@"C:\Users\SJD\Desktop\建立檔案夾",true);
Console.WriteLine("刪除成功");
Console.ReadKey();

3.沒有copy這個方法,但有move(剪下)
Directory.Move(@"C:\Users\SJD\Desktop\123", @"C:\Users\SJD\Desktop\456");
Console.WriteLine("OK");
Console.ReadKey();

4.Directory.GetFiles 擷取你指定的檔案夾下檔案的全路徑,後面加上*.格式,可以唯讀取選擇的格式檔案路徑
string[] path = Directory.GetFiles(@"C:\Users\SJD\Desktop\456", "*.jpg");
for (int i = 0; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
Console.ReadKey();

2、檔案流
兩個大水缸,把一個缸中的水倒入另一個水缸。兩種方式
.直接把一個缸中的水倒入另一個缸中。 file類
.用一個桶來把一個缸中的水舀到另一個缸中。 檔案流
需要建立對象

FileStream 操作位元組的
StreamReader StreamWriter 操作字元的
記憶體回收行程不會幫我們自動回收佔用的資源,必須要手動的close和dispose
但代碼一多總忘記加這兩個,所以我們把它寫在using裡面,讓他自動的協助我們釋放

FileStream fsRead 讀
using (FileStream fsRead=new FileStream(@"C:\Users\SJD\Desktop\123.txt",FileMode.OpenOrCreate,FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
//表示本次讀取實際讀取到的有效位元組數
int r= fsRead.Read(buffer, 0, buffer.Length);
string s= Encoding.Default.GetString(buffer, 0, r);
Console.WriteLine(s);
}
Console.ReadKey();

FileStream fsWrite 追加寫入
using (FileStream fsWrite=new FileStream(@"C:\Users\SJD\Desktop\234.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
string str = "今天天氣好晴朗";
byte[] buffer = Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, 0, buffer.Length);
Console.WriteLine("寫入成功");
}
Console.ReadKey();

StreamReader
using (FileStream fsRead=new FileStream(@"C:\Users\SJD\Desktop\123.txt",FileMode.OpenOrCreate,FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fsRead,Encoding.Default))
{
while(!sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
}
}
}
Console.ReadKey();

StreamWriter
byte[] buffer = new byte[1024 * 1024];
using (StreamWriter sw=new StreamWriter(@"C:\Users\SJD\Desktop\123.txt",true,Encoding.Default,buffer.Length))
{
sw.WriteLine("哈哈哈");
}
Console.WriteLine("OK");
Console.ReadKey();

3、序列化
要將序列化對象的類 標記為可以被序列化
[Serializable]
把對象序列化成二進位

序列化
using (FileStream fsWrite = new FileStream(@"C:\Users\SJD\Desktop\123.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fsWrite, p);
}
Console.WriteLine("序列化成功");
Console.ReadKey();

還原序列化
Person p;
using (FileStream fsRead = new FileStrea
m(@"C:\Users\SJD\Desktop\123.txt", FileMode.OpenOrCreate, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
p = (Person)bf.Deserialize(fsRead);
}
Console.WriteLine(p.Name);
Console.WriteLine(p.Age);
Console.WriteLine(p.Gender);
Console.ReadKey();

4、表單應用程式

C#基礎筆記(第二十一天)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.