Java attack c#--Syntax io operation

Source: Internet
Author: User

This chapter is a brief statement

In the previous chapter we explained the thread synchronization. Learn how to deal with dirty data that can occur. This chapter is about the operation classes that C # often uses when reading IO files. The content of this chapter will be relatively small. But the author still summed up to let the reader have a learning direction. Don't know what to learn.

File class

This is a static class. Can be said to be a tool class bar. He contains a lot of features about working with files. I'm just listing some of the features that are commonly used. More hope for the reader to understand it on their own.

1. Open a file stream . There are three ways to open it. The following code

public static FileStream open (string path, FileMode mode);p ublic static FileStream open (string path, FileMode mode, FILEAC Cess access);p ublic static FileStream Open (string path, FileMode mode, FileAccess access, FileShare share);

We can see that there are a total of four parameters.

Path parameter: Represents the file's paths.

FileMode parameter: Opens the file's mode. such as: New, start or new model. Readers are invited to view them on their own. You can also press F12 to view the comments inside the source code.

FileAccess parameter: Mode of operation. Whether it is read-only or read-write. or just write.

FileShare parameter: Indicates that after the operation is done. Readers are invited to view them on their own. You can also press F12 to view the comments inside the source code.

Take a look at the simple column I wrote. This is a convenient way to learn. As follows

 1 static void Main (string[] args) 2 {3 using (FileStream fs = File.Open ("TextFile1.txt", Filemo De. 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 one while (len = fs. Read (buffer, 0, buffer.                         Length)) > 0) {byte[] Tmpbuffer = new byte[len];14                     Array.copy (buffer, 0, Tmpbuffer, 0, tmpbuffer.length); Datalist.addrange (tmpbuffer); 16 }17 Console.WriteLine (Encoding.UTF7.GetString (Datalist.toarray (), 0, Datalist.count );}20}21 () Console.readkey (); 

The FileStream class is a file stream. With this class, we can read and write to the file. Equivalent to the Java FileInputStream class. In the above code we can see that the author has used an array class. He contains a lot of operations on the array. Readers can take a look at that. In the output, the author used the content of the encoding format. Encoding is the beginning of getting the encoding format. So be sure to remember the encoding class.

2. Memorize the contents of the file directly. we saw the contents of a file to read. We're going to do a lot of things. But the file class facts have made a simplified approach for us.

  Console.WriteLine (File.readalltext ("TextFile1.txt", Encoding.utf7));

All right. We have done so much more than what he has to say. A similar approach to him begins with read. Readers are invited to view them on their own. Similarly, we can see some ways to start with write. Nothing wrong. Is the meaning of writing. I believe I don't have to say more.

3. Delete the file. This feature is very common. It's simple, too.

File.delete ("TextFile1.txt");

4. Determine if there is any. The return ture represents the presence.

File.exists ("TextFile1.txt");
Directory Class

Directory, like the file class, is a static class. Only the file class is used to process files, and the directory class is used to work with directories. Speaking of which I believe we all understand. Here is why I did not say that the file class above is equivalent to the Java file class. But it's supposed to be two. The same as the Java file class.

1. Obtain the file under the directory.

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

2. Create the corresponding directory.

DirectoryInfo dir =  directory.createdirectory ("./test"), if (dir. Exists) {    Console.WriteLine ("Created successfully");} else{    Console.WriteLine ("Creation Failed");}

3. Determine if there is any.

if (Directory.Exists ("./test"))                Console.WriteLine ("Test directory already exists");
Path class

I have to say that when I operate IO, I often use the path class. He is also a static class. Don't underestimate him, he has some small functions really very good. The following are the functions that I often use.

1. Assemble the directory path.

String dir = Path.Combine (".", "Test");

2. Obtain a file name with no extension. He will help you remove the ". txt".

String name = Path.getfilenamewithoutextension ("TextFile1.txt");

3. Get the directory path inside the absolute path. It will return to "e:\\test\\" at this time.

String directorynameg = Path.getdirectoryname ("E:\\test\\textfile1.txt");

4. Determine if it is an absolute path.

Path.ispathrooted ("E:\\test\\textfile1.txt");
StreamReader class and StreamWriter class

I often use the StreamReader class to read text stream files. We can see that the following code has a symbol called @. This is used to indicate that the string after that is the path. This is what you can do without the "\ \". Just use "\" to do it.

using (StreamReader sr = new StreamReader (@ ". \textfile1.txt", Encoding.UTF8, True)) {while      (!SR. Endofstream)      {          Console.WriteLine (Sr.) ReadLine ());}      }

The StreamWriter class is often used to write.

using (FileStream fs = new FileStream (@ ". \textfile1.txt", FileMode.OpenOrCreate)) {       StreamWriter sw = new StreamWriter (FS);       Sw. WriteLine ("I am Aomi");       Sw. Flush ();}
XmlDocument class

When I develop applications, I often encounter the reading of XML configuration files. The XmlDocument class is often used in C #. He's just an action class that reads an XML file. Equivalent to the Saxreader class inside Java's dom4j. Most of the operations are very similar. Readers don't have to worry about that.

XmlDocument xmldoc = new XmlDocument (); Xmldoc.load ("./xmlfile1.xml"); XmlElement Xmlele = xmldoc.documentelement;if (xmlele.haschildnodes) {           
Console.WriteLine (Xmlele.selectsinglenode ("Name"). InnerText);}
This chapter summarizes

The content of this chapter will be relatively small. But I think I can not ignore it. In this chapter, you can say that the fundamentals of C # are all over. The subsequent chapters will be introduced into the Advanced section of C # application development.

Java attack c#--Syntax io operation

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.