1. Use filestream (which stores or reads data using Byte arrays or byte) 1.1 write data to the file:
String Path = "C :\\ test.txt ";
If (file. exists (PATH) // if the object exists, delete the object
File. Delete (PATH );
Filestream FS = file. Open (path, filemode. Create); // The second one indicates that if the file does not exist, a new
/// Filestream FS = file. Open (path, filemode. append); // If You Want to append the content, use this
FS. Write (BS, 0, BS. Length); // The BS here is an array byte []
FS. Close ();
1.2 read part of the file:
String STR = "C :\\ test.txt ";
If (! File. exists (STR) // check whether the file exists
{
MessageBox. Show ("the file does not exist. Check whether the client has uploaded data! ");
}
Else
{
Filestream fop = file. openread (STR );
Byte [] arr = new byte [1000];
While (FOP. Read (ARR, 0, arr. Length)> 0) // This loop reads all the content of the file.
{
Clientsocket [1]. Send (ARR, 0, arr. length, 0 );
}
FOP. Close ();
}
2. streamwriter and streamreader (the operation object is a character or string). Part 2.1 reads the file:
String Path = "C :\\ test.txt ";
String ST = "";
If (! File. exists (PATH ))
MessageBox. Show ("the file does not exist. Please create a new one first! ");
Else
{
Byte [] B = new byte [10];
Streamreader sr = new streamreader (PATH );
While (ST = Sr. Readline ())! = NULL)
{
TB. appendtext (ST );
MessageBox. Show (ST );
}
Sr. Close ();
}
2.2 file writing
If (file. exists (PATH ))
File. Delete (PATH );
Streamwriter Sw = new streamwriter (path, true); // true indicates that the content is added after the file exists, and false indicates that the content is overwritten.
Sw. Write ("Today is a good day! ");
Sw. Close ();
3. Use binaryreader and binarywriter for file read/write operations (any basic data type can be operated)
Binaryreader provides methods such as readstring, readbyte, readboolean, readint, and readdouble to read different types of data, while binarywriter provides write () method To write different types of values to the current stream (that is, the parameter of the write method can be any basic data type ). The following describes how to store and read data of the double type.
3.1 binarywrite write data
Filestream FS = file. Create (path1 );
Binarywriter BW = new binarywriter (FS );
Bw. Write (1, 100.123 );
Bw. Write (1, 200.524 );
Bw. Close ();
FS. Close ();
3.2 binaryreader reads data
Filestream fs1 = new filestream (path1, filemode. Open );
Binaryreader BR = new binaryreader (fs1 );
Console. writeline ("{0}", Br. readdouble ());
Console. writeline ("{0}", Br. readdouble ());
BR. Close ();
FS. Close ();