How to effectively use C # read files

xiaoxiao2021-03-06  47

How do you usually read a file? Use flow reading. Yes, C # gives us a very powerful class library (once again touted .Net), enclosed almost all we can think of and we didn't think of the class, the flow is a general means to read the document. So do you really use the data in your file? Can you really read it?

Usually we read a file using the following steps:

1, declare and use the file's OpenRead instantiated a file stream object, just like this

FILESTREAM FS = file.openread (filename);

or

FILESTREAM FS = fileStream (FileName, Filemode.open, FileAccess.Read, Fileshare.Read);

2. Prepare a byte array of file content, fs.length will get the actual size of the file, just like this

Byte [] data = new byte [fs.length];

3, wow! Start reading, call a method of a file stream to read data to the DATA array

fs.read (Data, 0, Data.length);

Ha ha! We only wrote 3 sentences to read the content of the files, it is too simple! Can this code really work like you expect? The answer is: almost! In most cases, the above code is working very well, but we should pay attention to the returning value, since there is a return value, if there is a truth, if the above is fully, it can be a function that does not return value. I want to return the value of the value is that in order to give us a chance to determine the size of the actual read file, it is to determine if the file is fully finished. So the above code can't guarantee that we must read all bytes inside the file (although it is read in many cases). The following method provides a method more secure than the above method to ensure that the file is fully read.

Public Static Void Saferead (stream stream, byte [] data) {

INT OFFSET = 0;

Int remaining = data.length;

// As long as there is the remaining byte, you keep reading

While (regaining> 0) {

INT read = stream.read (data, offset, remaining);

IF (read <= 0)

Throw new endofstreamexception ("File Read to" Read.toTString () "Failed!");

// reduce the number of bytes

REMAINING - = Read;

// increase the offset

OFFSET = Read;

}

}

In some cases, you don't know the actual length of the stream, such as: network stream. At this point, a similar method can be used to read the data until the data inside the stream is fully read. We can initially initialize a cache, then write the flow information to the memory flow, just like this:

Public static byte [] readfully (stream stream) {

// Initialize a 32K cache

Byte [] buffer = new byte [32768];

Using (MemoryStream MS = New MemoryStream ()) {// After the result is returned, the Dispose method to call the object is automatically reclaimed.

// Keep read

While (true) {

INT read = stream.read (buffer, 0, buffer.length);

/ / Until the last 3M

Data can return the result.

IF (read <= 0)

Return ms.toarray ();

Ms.write (buffer, 0, read);

}

}

}

Although the above example is relatively simple, the effect is not very obvious (most are right), maybe you have long, it doesn't matter whether this article is written to beginners.

The following method provides a stream using a specified cache length, although in many cases you can use stream.length to get the length of the stream, but not all the streams can be obtained.

Public Static Byte [] Read2Buffer (Stream Stream, Int Bufferlen) {

/ / If the specified invalid length buffer, specify a default length as a cache size

IF (bufferlen <1) {

Bufferlen = 0x8000;

}

// Initialize a buffer area

BYTE [] Buffer = New byte [bufferlen];

INT read = 0;

Int block;

/ / Read the cache size from the stream, know that reading all the streams

While ((block = stream.read (buffer, read, buffer.length-read)> 0) {

// Reset the read position

Read = block;

/ / Check if the border of the cache is reached, check if there is information that can be read

IF (read == buffer.length) {

// Try to read a byte

INT nextbyte = stream.readbyte ();

// Read failed, the read completion can return results

IF (NextByte == - 1) {

Return buffer;

}

// Adjust the array of size preparation 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), here is intended to reset the buffer pointer points to a larger memory

r ;

}

}

// If the cache is too large, use RET to shrink the buffer read in front of while, then return directly

Byte [] ret = new byte [read];

Array.copy (Buffer, Ret, Read);

Return Ret;

}

转载请注明原文地址:https://www.9cbs.com/read-67173.html

New Post(0)