开发者

FileStream Arguments Read/Write

In C++, you open a stream like so:

int variable = 45;

ifstream iFile = new ifstream("nameoffile"); // Declare + Open the Stream
       // iFile.open("nameofIle");

iFile >> variable;

iFile.close

I'm trying to understand C# FileStream. The read and write methods require an array and offset and count. This array how big is the array? Do I just give it any size and it will fill it up? If that's the case, how do I go about reading a file with Filestream? How do I know how bi开发者_如何学JAVAg of an array I pass in?


You can simply use StreamReader and StreamWriter wrappers to read and write:

using(StreamReader sr = new StreamReader(fileName))
{
   var value = sr.ReadLine(); // and other methods for reading
}



using (StreamWriter sw = new StreamWriter(fileName)) // or new StreamWriter(fileName,true); for appending available file
{
  sw.WriteLine("test"); // and other methods for writing
}

or do like the following:

StreamWriter sw = new StreamWriter(fileName);
sw.WriteLine("test");
sw.Close();


using (FileStream fs = new FileStream("Filename", FileMode.Open))
        {
            byte[] buff = new byte[fs.Length];
            fs.Read(buff, 0, (int)fs.Length);                
        }

Note that fs.Length is long and so you must check it like int.MaxValue < fs.Length.

Otherwise you use old method in a while loop (fs.Read returns the actual number of bytes read)

By the way FileStream WON'T fill it up but will throw an Exception instead.


While calling .Read method, you should specify and array, where resulting bytes will be stored. So, this array length should be at least (Index + Size). While writing, the same issue, except that these bytes will be get from array, not stored inside it.


The byte array in the arguments of the read method of the FileStream will get the bytes from the stream after reading, so the length should be equal to the length of the stream. From MSDN:

using (FileStream fsSource = new FileStream(pathSource,
            FileMode.Open, FileAccess.Read))
        {

            // Read the source file into a byte array.
            byte[] bytes = new byte[fsSource.Length];
            int numBytesToRead = (int)fsSource.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0)
                    break;

                numBytesRead += n;
                numBytesToRead -= n;
            }
             numBytesToRead = bytes.Length;

            // Write the byte array to the other FileStream.
            using (FileStream fsNew = new FileStream(pathNew,
                FileMode.Create, FileAccess.Write))
            {
                fsNew.Write(bytes, 0, numBytesToRead);
            }
        }

A streamreader is used to read text from a stream

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜