The 1:path class is designed to manipulate file paths (the path class is a static Class): Of course, the processing of strings can also be implemented.
string str = @ "C:\Users\ talent \desktop\hashtable.txt";
//Return file name
Console.WriteLine (Path.getfilename (str));
//Returns the file name, but does not include the extension
Console.WriteLine (path.getfilenamewithoutextension (str));
//Return the folder name where the file is located
Console.WriteLine (Path.getdirectoryname (str));
//Returns the file name extension
Console.WriteLine (path.getextension (str));
//Connect two strings as a path
Console.WriteLine (Path.Combine (@ "C:\a\", "B.txt"));
2:file This static class is used to process files (create, delete, copy, etc.)
file.create (@ "C:\a\b\d.txt");
File.delete (@ "C:\a\b\d.txt");
File.cope (@ "C:\a\b\d.txt");
encoding: Saves the string as a binary in what format.
garbled: The reason for the garbled is that you save the file in the encoding format and you open the file using the same encoding format.
text files: Files that you can read in a TXT file are text files.
3: Read the text file:
//Read 1.txt This file in UTF-8 encoded format
byte[] buffer= file.readallbytes (@ "C:\Users\ talent \desktop\1.txt");
string str=encoding.getencoding ("UTF-8"). GetString (buffer);
Console.WriteLine (str);
Console.readkey ();
4: Writes the file to the specified path:
//write to the file in the default format (ANSI).
string Str= "We all have a home, named China!!!!" ";
byte[] buffer=encoding.default.getbytes (buffer);
file.writeallbytes (@ "C:\Users\ \desktop\2.txt", buffer);
Console.readkey ();
5:file.readalllines (path, read encoding);//Returns an array of strings
File.readalltext (path, read encoding);//Return a string
the previous two methods can only read text files, can not read music files, multimedia files, etc.;
file.rradallbytes ();//Returns an array of Byte (byte) that can read various files, including music, video files.
6: Absolute path and relative path
absolute Path: This file can be found directly on my computer by the given path.
relative path: The path of the file relative to the application.
we use relative paths as much as possible in the development process;
7:file.writealllines (path, array of strings);
file.writealltext (path, string);
file.writeallbytes (path, byte array);//These three methods will overwrite something in the previous text.
8: There are several ways to add something to the back of the text:
file.appendalllines (path, array of strings);
file.appendalltext (path, string);
the file class can only be used to read small files, and to read large files requires a stream of files;
8: Generic list
list<t> list=new list<t> ();
list. Add ();
list. AddRange ();
list. Remove ();
list.removeat ();
list. Clear ();
list. Reverse ();
generics are much more convenient than arrays, and as soon as a type is determined it can be used as a dynamic array, and the generics and arrays can be converted to each other;
such as: int[] nums=list. Toarrray ();
string[] str={"A", "B", "C"};
list<string> listtwo= str. ToList ();
9: Packing, unpacking
Boxing: Converts a value type to a reference type.
Unboxing: Converts a reference type to a value type.
See whether the two types of boxing or unpacking, to see whether there is an inheritance relationship, if there is no inheritance relationship, there must be no boxing or unpacking, there is an inheritance relationship is possible to take place boxing or unpacking.
in our code, we try to avoid packing or unpacking, because it takes a long time to waste memory.
10:dictionary Dictionary
dictionary<int,string> dic=new dictionary<int,string> ();
DiC. ADD (1, "a");
DiC. ADD (2, "B");
DiC. ADD (3, "C");
dic[1]= "newly-arrived";
when traversing:
Foreach (Var item in DIC. Keys)
{
Console.WriteLine ("{0}----{1}", Item,dic[item]);
}
or is:
Foreach (keysvaluepair<int,string> kv in dic)
{
Console.WriteLine ("{0}----{1}", KV. Keys,kv. Values);
}
One :
The FileStream is an operation byte, so it can manipulate various files, including the text;
StreamReader and StreamWriter are manipulation characters, so only text files can be manipulated.
:
The process of creating a file stream object is written in the using, which automatically helps us to free up the space occupied by the stream.
StreamReader and StreamWriter are specifically used to manipulate files, if only for files, with StreamReader
and StreamWriter more convenient than the FileStream.
when exposed to the class of the flow, be sure to remember to write the parts of the object in the using, otherwise it will easily cause forgetting to release
resource, which causes a memory leak.
the advantage of using FileStream is that you can manipulate any type of file, but the middle involves the conversion of the encoding format.
C # Basic article--File (stream)