尝试在非常大的文件上执行LIFO文件IO [英] Trying to do LIFO file IO on very large file

查看:82
本文介绍了尝试在非常大的文件上执行LIFO文件IO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GOOD EVENING

我想使用LIFO(后进先出)方法读取一个非常大的terrabyte文件。

当前代码使用FIFO方法。

如何修改此代码以便为非常大的terrabyte文件执行LIFO?

谢谢



我的尝试:



GOOD EVENING
I want to read a very larg one terrabyte file using the LIFO (last in first out) method.
The current code uses the FIFO method.
How can this code be modified to do LIFO for a very large terrabyte files?
THANK YOU

What I have tried:

using System;
using System.IO;
using System.Collections;

namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Fol");
            if (!da.Exists)
            {
                Console.WriteLine("The folder '{0}' does not exist.", da.FullName);
                return;
            }
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                Console.WriteLine("There are no files in the folder '{0}'.", da.FullName);
                return;
            }
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);

            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));

            using (Stream input = File.OpenRead(filePath))
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bytesRead;
                while ((bytesRead = input.Read(buffer, 0, BufferSize)) > 0)
                {
                    for (int count = 0; count < bytesRead; count++)
                    {
                        byte theByte = buffer[count];
                        string theByteInBinary = Convert.ToString(theByte, 2).PadLeft(8, '0');
                        Console.WriteLine("{0} = {1}", theByteInBinary, theByte);
                    }
                    output.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}

推荐答案

你寻找文件中的最后一个字节并读取该字节。现在文件指针位于该字节之后。要读取下一个字节(在您刚刚读取的字节之前),您必须向后搜索两个字节并再次读取一个字节。这样做直到你完成或你试图寻求-1。



警告!这是一种非常低效的读取文件的方法,可能会永远占用1TB文件。



更好的方法是将字节块读入内存和进程这些块中的字节,逐字节,或者你需要。
You Seek to the last byte in the file and read that byte. Now the file pointer is sitting after that byte. To read the next byte (before the byte you just read) you have to Seek backwards two bytes and read one byte again. Do this until you're finished or you try to Seek to -1.

Warning! This is a very inefficient method of reading the file and will likely take forever on a 1TB file.

A better method would be to read blocks of bytes into memory and process the bytes in those blocks, byte-by-byte or however you need to.


这篇关于尝试在非常大的文件上执行LIFO文件IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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