Java attack C # -- syntax IO operations,

Source: Internet
Author: User

Java attack C # -- syntax IO operations,

Summary of this Chapter

In the previous chapter, we will explain thread synchronization. Learn how to process dirty data that may occur. This chapter describes the operation classes that C # often uses to read IO files. This chapter has less content. However, the author still sums up to give readers a learning direction. I don't know what to learn.

File class

This static class. It can be said to be a tool class. It also contains many file processing functions. I only list some common functions. We hope that readers can learn more about it on their own.

1. Open a file stream. There are three open methods. The following code

public static FileStream Open(string path, FileMode mode);public static FileStream Open(string path, FileMode mode, FileAccess access);public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share);

We can see that there are four parameters in total.

Path parameter: indicates the file path.

FileMode: the mode in which the file is opened. For example, new, open, or new mode. Read this information by readers. You can also press F12 to view the annotations in the source code.

FileAccess parameter: operation mode. Read-only or read-write. Or write only.

FileShare parameter: indicates that the operation is complete. Read this information by readers. You can also press F12 to view the annotations in the source code.

Let's take a look at the simple column written by the author. This facilitates learning. As follows:

 1 static void Main(string[] args) 2         { 3             using (FileStream fs = File.Open("TextFile1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 4             { 5                 if (fs.CanRead) 6                 { 7                     List<byte> dataList = new List<byte>(); 8                     byte[] buffer = new byte[1024]; 9                     int len = 0;10                   11                     while ((len = fs.Read(buffer, 0, buffer.Length)) > 0)12                     {13                         byte[] tmpBuffer = new byte[len];14                         Array.Copy(buffer, 0, tmpBuffer, 0, tmpBuffer.Length);15                         dataList.AddRange(tmpBuffer);16                     }17 18                     Console.WriteLine(Encoding.UTF7.GetString(dataList.ToArray(), 0, dataList.Count));19                 }20             }21 22             Console.ReadKey();23 24         }

The FileStream class is a file stream. With this class, we can read and write files. It is equivalent to the FileInputStream class of JAVA. In the code above, we can see that the author uses an Array class. It contains many operations on arrays. Readers can take a look. During the output, I used the encoding format. Encoding is the start to get the Encoding format. So remember the Encoding class.

2. Directly remember the file content.We can see the content of a file to be read. We have to do a lot of things. But the fact of the File class makes a simplified method for us.

  Console.WriteLine(File.ReadAllText("TextFile1.txt", Encoding.UTF7));

Okay. So many things we have done above are not as good as a sentence. Similar methods are all started with Read. Read this information by readers. Similarly, we can see some methods starting with Write. No error. Is the meaning of writing. I don't need to talk about it.

3. delete an object.This function is very common. It is also very simple

File.Delete("TextFile1.txt");

4. determine whether or not it exists.Returns true to indicate that the specified image exists.

File.Exists("TextFile1.txt");
Directory

The Directory class is a static class like the File class. The File class is used to process files, while the Directory class is used to process directories. I believe everyone understands this. This is why I didn't say that the above File class is equivalent to the File class of JAVA. But it is true that they should be added together, which is equivalent to the File class of JAVA.

1. Get the files in the directory.

 string[] fileNames = Directory.GetFiles(".");

2. Create a directory.

DirectoryInfo dir = Directory. createDirectory (". /test "); if (dir. exists) {Console. writeLine ("created successfully");} else {Console. writeLine ("failed to create ");}

3. determine whether or not it exists.

If (Directory. Exists ("./test") Console. WriteLine ("the test Directory already Exists ");
Path class

I have to say that the Path class is often used during I/O operations. It is also a static class. Don't underestimate him. He has some small functions that are really good. The following are the features that I often use.

1. Assemble the directory path.

 string dir = Path.Combine(".", "test");

2. Get the file name without the extension. He will help you drop the warning .txt ".

 string name = Path.GetFileNameWithoutExtension("TextFile1.txt");

3. Obtain the directory path in the absolute path. At this time, "E: \ test \" is returned \\"

string directoryNameg = Path.GetDirectoryName("E:\\test\\TextFile1.txt");

4. Determine whether the path is an absolute path.

Path.IsPathRooted("E:\\test\\TextFile1.txt");
StreamReader and StreamWriter

The StreamReader class is often used to read text streams. The following code contains the @ symbol. This string is used to indicate the path. You don't need to use. You only need to use.

using (StreamReader sr = new StreamReader(@".\TextFile1.txt", Encoding.UTF8, true)){      while (!sr.EndOfStream)      {          Console.WriteLine(sr.ReadLine());      }}

StreamWriter is often used to write data.

using (FileStream fs = new FileStream(@".\TextFile1.txt", FileMode.OpenOrCreate)){       StreamWriter sw = new StreamWriter(fs);       sw.WriteLine("i am aomi");       sw.Flush();}
XmlDocument class

When developing applications, I often encounter reading xml configuration files. The XmlDocument class is often used in C. It is an operation class for reading Xml files. It is equivalent to the SAXReader class in JAVA Dom4j. Most operations are similar. Readers don't have to worry.

XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load("./XMLFile1.xml");XmlElement xmlEle = xmlDoc.DocumentElement;if (xmlEle.HasChildNodes){           
  Console.WriteLine(xmlEle.SelectSingleNode("Name").InnerText);}
Summary

This chapter has fewer contents. However, I do not think it can be ignored. In this chapter, we can say that the basic knowledge about C # is all over. The subsequent sections will go to the advanced section of C # application development.

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.