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 );
// Clear the buffer and close 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 ();
========================================================== ========================================================== ===
Method 1: Use FileStream
// Instantiate a save file dialog box
SaveFileDialog sf = new SaveFileDialog ();
// Set the file storage type
Sf. Filter = "txt file | *. txt | all files | *.*";
// If the user does not enter the extension, the suffix is automatically appended.
Sf. AddExtension = true;
// Set the title
Sf. Title ="Write files";
// If the user clicks the Save button
If (sf. ShowDialog () = DialogResult. OK)
{
// Instantiate a file stream ---> associated with the written file
FileStream fs = new FileStream (sf. FileName, FileMode. Create );
// Obtain the byteArray
Byte[] Data = new UTF8Encoding (). GetBytes (this. textBox1.Text );
// Start writing
Fs. Write (data, 0, data. Length );
// Clear the buffer and close the stream
Fs. Flush ();
Fs. Close ();
}
Method 2: Use StreamWriter
// Instantiate a save file dialog box
SaveFileDialog sf = new SaveFileDialog ();
// Set the file storage type
Sf. Filter = "txt file | *. txt | all files | *.*";
// If the user does not enter the extension, the suffix is automatically appended.
Sf. AddExtension = true;
// Set the title
Sf. Title ="Write files";
// If the user clicks the Save button
If (sf. ShowDialog () = DialogResult. OK)
{
// Instantiate a file stream ---> associated with the written file
FileStream fs = new FileStream (sf. FileName, FileMode. Create );
// Instantiate a StreamWriter --> associated with fs
StreamWriter sw = new StreamWriter (fs );
// Start writing
Sw. Write (this. textBox1.Text );
// Clear the buffer
Sw. Flush ();
// Close the stream
Sw. Close ();
Fs. Close ();
}
String FileName = Guid. NewGuid (). ToString () + ". txt"; // GUID generated unique file name
StringBuilder ckpw = new StringBuilder ("credential output", "V800", "001 ","