1. Use FileStream to read and write files File Header:Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;Core code for reading files:Byte [] byData = new byte [100];
Char [] charData = new char [1000];
Try
{
FileStream sFile = new FileStream ("file path", FileMode. Open );
SFile. Seek (55, SeekOrigin. Begin );
SFile. read (byData, 0,100); // The first parameter is the byte array that is passed in to accept the data in the FileStream object. The second parameter is the position where data is written in the byte array, it is usually 0, indicating to write data to the array from the array's start file. The last parameter specifies the number of characters read from the file.
}
Catch (IOException e)
{
Console. WriteLine ("An IO exception has been thrown! ");
Console. WriteLine (e. ToString ());
Console. ReadLine ();
Return;
}
Decoder d = Encoding. UTF8.GetDecoder ();
D. GetChars (byData, 0, byData. Length, charData, 0 );
Console. WriteLine (charData );
Console. ReadLine ();Core code for writing files:FileStream fs = new FileStream (file path, FileMode. Create );
// Obtain the byte array byte [] data = new UTF8Encoding (). GetBytes (String );
// Start writing
Fs. Write (data, 0, data. Length); // clears the buffer and closes the stream.
Fs. Flush ();
Fs. Close ();2. Use StreamReader and StreamWriter File Header:Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;StreamReader reads files:StreamReader objReader = new StreamReader (file path );
String sLine = "";
ArrayList LineList = new ArrayList ();
While (sLine! = Null)
{
SLine = objReader. ReadLine ();
If (sLine! = Null &&! SLine. Equals (""))
LineList. Add (sLine );
}
ObjReader. Close ();
Return LineList;StreamWriter writes files:FileStream fs = new FileStream (file path, FileMode. Create );
StreamWriter sw = new StreamWriter (fs );
// Start writing
Sw. Write (String );
// Clear the buffer
Sw. Flush ();
// Close the stream
Sw. Close ();
Fs. Close ();
Read. txt files with csung.
StreamReader sr = new StreamReader ("TestFile.txt") // StreamReader sr = new StreamReader ("TestFile.txt", Encoding. GetEncoding ("GB2312 "))
/// GBK
String line;
While (line = sr. ReadLine ())! = Null)
{
TextBox1. Text + = ii. ToString () + "-" + line. ToString () + "";
}
Add reference:System. IO
StreamReader objReader = new StreamReader ("c: \ test.txt ");
System. IOObjects in a namespace, especiallySystem. IO. StreamReaderClass.
It is generally used together to represent the Enter key on the keyboard. It can also be used only to represent the "TAB" key on the keyboard.