西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)C#.NET → C#中的文件流StreamReader、StreamWriter和File類等操作學(xué)習(xí)

C#中的文件流StreamReader、StreamWriter和File類等操作學(xué)習(xí)

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:韓迎龍時(shí)間:2012/10/10 8:53:07字體大小:A-A+

作者:韓迎龍點(diǎn)擊:2259次評(píng)論:0次標(biāo)簽: 文件流

  • 類型:編程輔助大。808KB語(yǔ)言:英文 評(píng)分:3.3
  • 標(biāo)簽:
立即下載

這篇主要介紹幾個(gè)操作文件流的類,讀寫(xiě)類StreamReader,StreamWriter和File類以及Directory類的操作

FileStream類的控制

(1) Flush();  清除此流的緩沖區(qū),是為了保護(hù)硬盤(pán)

PSE: collapse; HEIGHT: auto! important; TEXT-ALIGN: left! important; outline: 0px; box-sizing: content-box; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-spacing: 0px" cellspacing="0" cellpadding="0" border="0">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static void Main(string[] args)
{
            using (FileStream filewrite = new FileStream("file.txt", FileMode.Create, FileAccess.Write))
            {
                filewrite.WriteByte(101);
                filewrite.WriteByte(101);
                //清除此流的緩沖區(qū)
                filewrite.Flush();
                filewrite.WriteByte(101);
                filewrite.WriteByte(101);
                //每次寫(xiě)一個(gè)直接就會(huì)頻繁的操作硬盤(pán),
            }
}

(2)Seek(偏移,位置枚舉)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void Main(string[] args)
{
            using (FileStream fileRead = new FileStream("file.txt", FileMode.Open, FileAccess.Read))
            {
                fileRead.Position = 4;
                fileRead.Seek(3, SeekOrigin.Current);
                int n = fileRead.ReadByte();
                Console.WriteLine((char)n);
            }

其它流

(1)MemoryStream 內(nèi)存流

            NetworkStream ns = new NetworkStream();

(2)NetworkStream 網(wǎng)絡(luò)流

            MemoryStream ms = new MemoryStream();

讀寫(xiě)流

(1) StreamReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//讀取文件流
        static void Main(string[] args)
        {
            using(FileStream fileRead=new FileStream("成功.txt",FileMode.Open,FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fileRead,Encoding.Default))
                {
                    //第一種讀法
                    //string str = sr.ReadLine();
                    //while ((str = sr.ReadLine()) != null)
                    //{
                    //    Console.WriteLine(str);
                    //}
                    //第二種讀法
                    Console.WriteLine(sr.ReadToEnd());
                }
            }
        }

(2)StreamWriter

            static void Main(string[] args)

        {

            using (StreamWriter sw = new StreamWriter("成功.txt"))

            {

                sw.WriteLine();

            }

        }

File

(1) File關(guān)于文件讀取的方法

            1)使用File文件讀取流

                   //損失性能

            byte[] bs=File.ReadAllBytes("file.txt");

            2)管理文件

                   ->創(chuàng)建文件

                          File.Create("f:\\韓迎龍.txt", 10 * 1024 * 1024);

                   ->刪除文件

                          File.Delete("f:\\韓迎龍.txt");

                   ->查文件

                          bool isExist = File.Exists("f:\\韓迎龍.txt");

                          Console.WriteLine(isExist);

(2)FileInfo

            1)創(chuàng)建文件

                   FileInfo file = new FileInfo("f:\\韓迎龍.txt"); //在內(nèi)存中存在

            file.Create();  //創(chuàng)建文件

            2)設(shè)置屬性

                   file.Attributes = FileAttributes.ReadOnly; //在屬性中查看

(3)Copy方法 復(fù)制

            File.Copy("f:\\韓迎龍.txt", "f:\\111.txt");

(4)Move方法 移動(dòng)

            File.Move("f:\\韓迎龍.txt", "f:\\1\\韓迎龍.txt");

(5)修改文件的全部名稱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
static void Main(string[] args)
        {
            string[] fnames = Directory.GetFiles("f:\\1");
            //Array的Sort方法可以對(duì)數(shù)組進(jìn)行排序
            Array.Sort(fnames);
            for (int i = 0; i < fnames.Length; i++)
            {
                string temp = fnames[i];
                //獲得文件名
                string fileName = Path.GetFileName(temp);
                //獲得路徑名
                string path = Path.GetDirectoryName(temp);
                //新的文件名
                string newPath = Path.Combine(path, i.ToString(new string('0', fnames.Length.ToString().Length)) + ".txt");
                File.Move(fnames[i], newPath);
            }
        }

Directory

(1) 增

            //創(chuàng)建文件夾

        Directory.CreateDirectory("F:\\2.exe");

(2)刪

            Directory.Delete("f:\\1", true); //直接刪除,在回收站中再找不到了

(3)和File的使用基本一樣  

(4)得到文件夾下面的子文件夾

            string[] subDir=Directory.GetDirectories("文件夾路徑");

(5)得到文件夾下面的所有子文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   string[] file= Directory.GetFiles("文件夾路徑");
  //先創(chuàng)建幾個(gè)文件
 string type = "mp3|mp4|doc|rmvb|txt|xls|exe|avi";
  string path = @"F:\file";
 if(!Directory.Exists(path))
 {
      Directory.Create(path);
 }
Random rand=new Random();
string[] ts=type.Split('|');
for (int i = 0; i < 100; i++)
{
    File.Create(Path.Combine(path, Path.ChangeExtension(i.ToString(), ts[rand.Next(ts.Length)])));
 }  
  string[] files = Directory.GetFiles(@"F:\file", "*.txt");

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門(mén)評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)