How do I use File.ReadAllBytes In chunks

后端 未结 5 1178
北恋
北恋 2020-12-22 14:40

I am using this code

        string location1 = textBox2.Text;
        byte[] bytes = File.ReadAllBytes(location1);
        string text = (Convert.ToBase64S         


        
相关标签:
5条回答
  • 2020-12-22 15:15

    Depending on the file structure, it might be easier for you to use a StreamReader which exposes a ReadLine method.

    using(var sr = new StreamReader(File.Open(textBox2.Text, FileMode.Open))
    {
        while (sr.Peek() >= 0) 
        {
            Console.WriteLine(sr.ReadLine());
         }
    }
    
    0 讨论(0)
  • 2020-12-22 15:21
    private void button1_Click(object sender, EventArgs e)
    {
        const int MAX_BUFFER = 2048;
        byte[] Buffer = new byte[MAX_BUFFER];
        int BytesRead;
        using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
            while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
            {
                string text = (Convert.ToBase64String(Buffer));
                textBox1.Text = text;
    
            }
    }
    
    0 讨论(0)
  • 2020-12-22 15:24

    If the file is a text file you can use a TextReader:

       string location1 = textBox2.Text;
        string text  = String.Empty;
        using (TextReader reader = File.OpenText(location1 ))
        {
               do
               {
               string line = reader.ReadLine();
                   text+=line;
                }
                while(line!=null)
    
        }
    
    0 讨论(0)
  • 2020-12-22 15:28

    The bytesRead is just the count of bytes read.

    Here is some block reading

            var path = @"C:\Temp\file.blob";
    
            using (Stream f = new FileStream(path, FileMode.Open))
            {
                int offset = 0;
                long len = f.Length;
                byte[] buffer = new byte[len];
    
                int readLen = 100; // using chunks of 100 for default
    
                while (offset != len)
                {
                    if (offset + readLen > len)
                    {
                        readLen = (int) len - offset;
                    }
                    offset += f.Read(buffer, offset, readLen);
                }
            }
    

    Now you have the bytes in the buffer and can convert them as you like.

    for example inside the "using stream":

                string result = string.Empty;
    
                foreach (byte b in buffer)
                {
                    result += Convert.ToChar(b);
                }
    
    0 讨论(0)
  • 2020-12-22 15:36

    No, the return value from Read() is how many bytes were read. The data is in the byte array buf that you are passing to Read(). You should try and understand code instead of just copy & pasting, then asking why it doesn't work. Even if you do, the comment says it right there!

    0 讨论(0)
提交回复
热议问题