FileStream.Read - 为什么我需要循环读取的字节数? [英] FileStream.Read - Why I need to loop the number of bytes to read?

查看:230
本文介绍了FileStream.Read - 为什么我需要循环读取的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从MSDN获得以下代码。



所以,我不明白为什么我必须循环FileStream.Read,当它返回与文件相同的值时每次都是长度。

numBytesToRead将结束为零,FileStream.Read只执行一次。

评论说read可以返回从0到numBytesToRead的任何内容,这意味着从偏移到数量吧?

那么有人可以解释为什么以及偏移和计数的功能是什么?

先谢谢。



  public   static   void  Main()
{
// 指定一个文件到读取和创建。
string pathSource = @ C:\tests\source.txt;
string pathNew = @ c:\\ \\tests\\\
ewfile.txt;

尝试
{

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

// 将源文件读入字节数组。
byte [] bytes = new 字节 [fsSource.Length];
int numBytesToRead =( int )fsSource.Length;
int numBytesRead = 0 ;
while (numBytesToRead > 0
{
// 读取可能会返回任何内容0到numBytesToRead。
int n = fsSource.Read(bytes,numBytesRead,numBytesToRead);

// 到达文件末尾时中断。
if (n == 0
断裂;

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

// 将字节数组写入另一个FileStream。
使用(FileStream fsNew = new FileStream(pathNew,
FileMode.Create,FileAccess.Write))
{
fsNew.Write(bytes, 0 ,numBytesToRead);
}
}
}
catch (FileNotFoundException ioEx)
{
Console.WriteLine (ioEx.Message);
}
}

解决方案

您好b $ b

您可以访问以下链接了解详情

http:// msdn .microsoft.com / zh-cn / library / system.io.filestream.read.aspx [ ^ ]


谁告诉你需要循环任何东西?这完全取决于您,取决于您需要做的操作。请记住,如果文件足够小以适合内存,则根本不需要直接使用文件流,因为您可以使用 System.IO.File 方法 ReadAllBytes ReadAllLines ReadAllText

http://msdn.microsoft.com/en-us/library/system。 io.file.aspx [ ^ ]。



因此,只有在需要阅读文件的一小部分时才需要直接使用文件流或者另一种算法,并不总是一个愚蠢的循环。



在获取文件流之前,我想指出,在大多数情况下,你应该更好地使用这些类 System.IO.StreamReader System.IO.StreamWriter System.IO.BinaryReader System.IO.BinaryWriter

http://msdn.microsoft.com/en-us/library/vstudio/system。 io.streamreader [ ^ ],

http:// msdn。 microsoft.com/en-us/library/vstudio/system.io.streamwriter [ ^ ],

http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx [ ^ ],

http:// msdn.mic rosoft.com/en-us/library/system.io.binarywriter.aspx [ ^ ]。



通常,您需要在更复杂的情况下直接使用文件流,你需要在同一个流上进行非常不规则的读/写或读写操作。



现在,我们需要返回 System.IO.FileStream.Read 操作。然后返回实际读取的字节数。你为什么需要它?因为你可以命中文件的末尾。在这种情况下,您不需要计算要读取的字节数。您读取了一些块并返回实际读取的字节数。这是合法的操作;它不会抛出异常。而且,您不仅不想打扰文件末尾的字节数;有时您无法知道原则上可以读取多少字节。在一些相对罕见的情况下,您没有对某些流的独占访问权限。说,你读它,其他一些线程甚至进程修改流。请记住,原始 FileStream 专为更棘手的操作而设计...



-SA

Got the code below from MSDN.

So, I don't understand why I had to loop FileStream.Read when it return the same value as the file length everytime.
The numBytesToRead will end up zero and FileStream.Read only executed once.
The comment said that "read may return anything from 0 to numBytesToRead", that means from offset to count right?
So can someone explain why and what's the function of offset and count?
Thanks before.

public static void Main()
{
    // Specify a file to read from and to create. 
    string pathSource = @"c:\tests\source.txt";
    string pathNew = @"c:\tests\newfile.txt";

    try
    {

        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);
            }
        }
    }
    catch (FileNotFoundException ioEx)
    {
        Console.WriteLine(ioEx.Message);
    }
}

解决方案

Hi
You can visit the below link for details
http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx[^]


Who told you that you need to loop anything? It's totally up to you and depends on the operation you need to do. Remember that if the file is small enough to fit in memory, you don't need to directly use file stream at all, because you could use System.IO.File methods ReadAllBytes, ReadAllLines or ReadAllText:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx[^].

Therefore, the direct use of file streams is only required when you need to read just some small part of the file, using one or another algorithm, and not always a dumb loop.

Before getting to file streams, I would like to note that, in most cases, you should better use the classes System.IO.StreamReader, System.IO.StreamWriter, System.IO.BinaryReader or System.IO.BinaryWriter:
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^].

Usually you need to use file streams directly in more complex cases, when you need very irregular read/write or read and write operations on the same stream.

Now, we're coming to the need of the return of the System.IO.FileStream.Read operation. It returns then number of actually read bytes. Why do you need it? Because you can hit the end of file. In this case, you don't need to calculate how many bytes to read. You read some chunk and return the actually read number of bytes. This is a legitimate operation; it won't throw an exception. Moreover, not only you don't want to bother about number of the bytes to the end of the file; sometimes you cannot know how many bytes you can read in principles. In some relatively rare cases, you don't have exclusive access to some streams. Say, you read it, and some other thread or even process modifies the stream. Remember, raw FileStream is designed for more tricky operations…

—SA


这篇关于FileStream.Read - 为什么我需要循环读取的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆