Article 2 of file stream learning notes in C #

Source: Internet
Author: User

In this blog, I will talk about some other file streams in C # based on the previous article, this article describes several File stream operations, including reading and writing StreamReader, StreamWriter, File, and Directory operations.

  1. FileStream class control

(1) Flush (); clears the buffer for this stream to protect the hard disk

Static void Main (string [] args) {using (FileStream filewrite = new FileStream ("file.txt", FileMode. create, FileAccess. write) {filewrite. writeByte (101); filewrite. writeByte (101); // clears the buffer filewrite for this stream. flush (); filewrite. writeByte (101); filewrite. writeByte (101); // the hard disk is frequently operated upon each write ,}}

(2) Seek (offset, position enumeration)

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. Other streams

(1) memory stream of MemoryStream

MemoryStream MS = new MemoryStream ();

(2) NetworkStream network stream

NetworkStream ns = new NetworkStream ();

  1. Read/write stream

(1) StreamReader

// Read the static void Main (string [] args) {using (FileStream fileRead = new FileStream ("Success .txt", FileMode. open, FileAccess. read) {using (StreamReader sr = new StreamReader (fileRead, Encoding. default) {// first reading method // string str = sr. readLine (); // while (str = sr. readLine ())! = Null) // {// Console. WriteLine (str); //} // method 2 Console. WriteLine (sr. ReadToEnd ());}}}

(2) StreamWriter

Static void Main (string [] args)

{

Using (StreamWriter sw = new StreamWriter ("Success .txt "))

{

Sw. WriteLine ();

}

}

  1. File

(1) File Reading Method

1) Use a File to read the stream

// Performance loss

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

2) File Management

-> Create a file

File. Create ("f: \ Han Yinglong .txt", 10*1024*1024 );

-> Delete an object

File. Delete ("f: \ Han Yinglong .txt ");

-> File query

Bool isExist = File. Exists ("f: \ Han Yinglong .txt ");

Console. WriteLine (isExist );

(2) FileInfo

1) create a file

FileInfo file = new FileInfo ("f: \ Han Yinglong .txt"); // The file exists in the memory.

File. Create (); // Create a file

2) Set Properties

File. Attributes = FileAttributes. ReadOnly; // view the Attributes

(3) Copy method

File. Copy ("f :\\ Han Yinglong .txt", "f :\\ 111.txt ");

(4) Move the Move Method

File. Move ("f :\\ Han Yinglong .txt", "f :\\ 1 \ Han Yinglong .txt ");

(5) modify all file names

Static void Main (string [] args) {string [] fnames = Directory. getFiles ("f: \ 1"); // The Sort method of Array can Sort the Array. sort (fnames); for (int I = 0; I <fnames. length; I ++) {string temp = fnames [I]; // obtain the file name string fileName = Path. getFileName (temp); // obtain the path string Path = path. getDirectoryName (temp); // new file name string newPath = Path. combine (path, I. toString (new string ('0', fnames. length. toString (). length) + ". txt "); File. move (fnames [I], newPath );}}

  1. Directory

(1) Increase

// Create a folder

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

(2) Delete

Directory. Delete ("f: \ 1", true); // directly Delete the file. The file cannot be found in the recycle bin.

(3) The use of File is basically the same.

(4) obtain the subfolders under the folder.

String [] subDir = Directory. GetDirectories ("folder path ");

(5) obtain all the sub-files in the folder.

String [] file = Directory. getFiles ("folder path"); // create several files first 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 ");
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.