C #, use the File and FileStream ---- reprint

File and FileStream in C # the usage of
original Yi Xi Chen Posted on 2019-04-10 11:34:23 read the number 5841 collection
expanded

found his foundation is weak in the recent course of their work, so recently the basics of catching up. Here I learned to File and FileStream class simple summary.
1. First introduce the File class and file stream FileStream

1.1 File class is a static class that supports basic file operations, including creating, copying, moving, deleting and open a file. Parameter File class methods are often the path path. Mainly to provide relevant documents various operations required when using the System.IO namespace references.
 1.2 FileStream raw bytes can only handle file stream (raw byte). FileStream class can be used for any data files, not just text files. FileStream object can be used to read documents such as images and sound, FileStream read out of the byte array and then converting the conversion by the encoding byte array into a string.

1.3 difference: file: is the class of a file, the file operation; filestream: tool when a file stream to write the contents of txt, xml and other files you want to use to make a vivid metaphor .file notebook need. filestream this pen to write.
2. Code section

created Txt 2.1 File class

       String path = @ "C: \ the Users \ Administrator \ Desktop \ 1.txt";
       File.Open (path, FileMode.OpenOrCreate);

2.2 class Txt File stitching content

       List <String> the Users = new new List <String> ();
                users.Add ( "Joe Smith | 1000");
                users.Add ( "John Doe | 10000");
                users.Add ( "Wang Wu | 5000");
                users.Add ( "Zhao six | 6000");
                 File.AppendAllLines (path, Users, Encoding.Default);

2.3 File class byte read as a txt file

    byte [] = buttf File.ReadAllBytes (path);
    String STR = Encoding.Default.GetString (buttf, 0, buttf.Length);

2.4 File class implementation txt wages doubled

      String [] = filestr File.ReadAllLines (path, Encoding.Default);
       for (int I = 0; I <filestr. the Length; I ++)
         {
            String [] filestr STR = [I] .split ( '|');
            filestr [I] STR = [0] + "|" + (Convert.ToDouble (STR [. 1]) * 2) .ToString ();
          }
         File.AppendAllLines (path, filestr, Encoding.Default);

2.5 Common File Class method

3. file stream

3.1 in the form of text file stream read

     byte [] = bytsize new new byte [1024 * 1024 *. 5];
                the using (the fileStream = new new stream the fileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    the while (to true )
                    {
                        int = R & lt Stream.Read (bytsize, 0, bytsize.Length);
                        // if the number of bytes read is 0, indicating that it has reached the end of file, exit the while circulating
                        IF (R & lt == 0)
                        {
                            BREAK ;
                        }
     
                        String str = Encoding.Default.GetString (bytsize, 0, r);
                        Console.WriteLine (str);
                    }
                }

3.2 file stream in the form of written text

     string str = "sunny weather is good today, good scenery everywhere";
                byte [] = Encoding.Default.GetBytes buttf (STR);
                // stream file writing
                the using (the FileStream = new new fscreat the FileStream (path, FileMode.APPEND, FileAccess.Write))
                {
     
                    fscreat.Write (buttf, 0, buttf.Length) ;
                }

3.3 in the form of streaming video file copy

     byte [] = bytsize new new byte [1024 * 1024 *. 5];       
    String videoPath with an @ = "C: \ the Users \ Administrator \ Desktop \. 1, practice 1-3.avi";
                = @ videoNewPath String "C: \ the Users \ Administrator \ Desktop \ 1.avi";
                // Create a stream reading
                the using (the FileStream = new new FSRead the FileStream (videoPath with an, FileMode.Open, FileAccess.Read))
                {
                    // create a written stream
                    the using (the FileStream = new new fswrite the FileStream (videoNewPath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                          the while (to true)
                      {
                        // bytes actually read is returned to the
                        int r = fsread.Read (bytsize, 0 , bytsize.Length );
                       // bit 0 byte time when the proof reading has ended
                        iF (R & lt == 0) {
                            BREAK;
                        }
                        Fswrite.Write (bytsize, 0, r);
      
                    }
                 }
                    
                }

Because just started to learn some less so, I hope you can correct me big brother.
----------------
Disclaimer: This article is the original article CSDN bloggers "Chen Yi Xi", the follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/qq_41209575/article/details/89178020

Guess you like

Origin www.cnblogs.com/bedfly/p/12130345.html