To read an object, follow these steps:
1. Declare and use the OpenRead of File to instantiate a File stream object, as shown below:
2. Prepare a byte array for storing the file content. fs. Length will get the actual size of the file, as shown below:
3. Start reading. Call a method of a file stream to read data into the data array.
FileStream fs = File. OpenRead (filename); or
FileStream fs = FileStream (filename, FileMode. Open, FileAccess. Read, FileShare. Read );
Byte [] data = new byte [fs. Length];
Fs. Read (data, 0, data. Length );
The following method provides a safer method than the preceding method to ensure that the file is fully read.
Public static void SafeRead (Stream stream, byte [] data)
{
Int offset = 0;
Int remaining = data. Length; // read continuously as long as the remaining bytes exist.
While (remaining> 0)
{
Int read = stream. Read (data, offset, remaining );
If (read <= 0)
Throw new EndOfStreamException ("File read to" + read. ToString () + "failed! "); // Reduce the remaining bytes
Remaining-= read; // increase the offset
Offset + = read;
}
}
In some cases, you do not know the actual length of a stream, for example, a network stream. In this case, you can use a similar method to read the stream until the data in the stream is fully read. We can initialize a cache first, and then write the stream information read from the stream to the memory stream, as shown below:
Public static byte [] ReadFully (Stream stream)
{
// Initialize a 32 K Cache
Byte [] buffer = new byte [32768];
Using (MemoryStream MS = new MemoryStream ())
{
// After the returned results are returned, the Dispose method of the object will be automatically recycled to release the memory // read continuously
While (true)
{
Int read = stream. Read (buffer, 0, buffer. Length); // The result is returned until the last 3 M data is read.
If (read <= 0)
Return ms. ToArray (); ms. Write (buffer, 0, read );
}
}
}
Although the above examples are relatively simple and the results are not very obvious (most of them are correct), you may have been there for a long time. It doesn't matter whether this article was originally intended for beginners. The following method provides a way to read a Stream by specifying the cache length, although in many cases you can directly use Stream. length indicates the Length of the stream, but not all streams can.
Public static byte [] Read2Buffer (Stream stream, int BufferLen)
{
// If the specified buffer with invalid length is specified, a default length is specified as the cache size.
If (BufferLen <1)
{BufferLen = 0x8000;} // initialize a cache Zone
Byte [] buffer = new byte [BufferLen];
Int read = 0; int block; // read cached data from the stream every time until all the streams are read.
While (block = stream. Read (buffer, read, buffer. Length-read)> 0)
{
// Reset the read location
Read + = block; // check whether the cache boundary is reached and whether there is any information that can be read.
If (read = buffer. Length)
{
// Try to read one byte
Int nextByte = stream. ReadByte (); // If the read fails, the result is returned after the read is completed.
If (nextByte =-1)
{
Return buffer;
} // Adjust the array size to continue reading
Byte [] newBuf = new byte [buffer. Length * 2]; Array. Copy (buffer, newBuf, buffer. Length );
NewBuf [read] = (byte) nextByte; buffer = newBuf;
// Buffer is a reference (pointer), which is intended to reset the buffer pointer to a larger memory.
Read ++;
}
} // If the cache is too large, ret is used to shrink the buffer read while before and then return directly
Byte [] ret = new byte [read]; Array. Copy (buffer, ret, read );
Return ret;
}
Using System;
Using System. IO;
Using System. Collections;
Namespace TextFileReader_csharp
{
Class Class1
{
Static void Main (string [] args)
{
StreamReader objReader = new StreamReader ("c: \ test.txt ");
String sLine = "";
ArrayList arrText = new ArrayList ();
While (sLine! = Null)
{
SLine = objReader. ReadLine ();
If (sLine! = Null)
ArrText. Add (sLine );
}
ObjReader. Close ();
Foreach (string sOutput in arrText)
Console. WriteLine (sOutput); Console. ReadLine ();
}
}
}