Reading parts of large files from drive

前端 未结 3 1097
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 16:30

I\'m working with large files in C# (can be up to 20%-40% of available memory) and I will only need small parts of the files to be loaded into memory at a time (like 1-2% of

相关标签:
3条回答
  • 2020-12-19 16:49

    I know nothing about the structure of these files, but reading a portion of a file with FileStream or similar sounds like the best and fastest way to do it.

    You will not need to copy the byte[] since FileStream can read directly into a byte array.

    It sounds like you might know more about the structure of the file, which could bring up additional techniques as well. But if you need to read only a portion of the file, then this would probably be the way to do it.

    0 讨论(0)
  • 2020-12-19 17:00

    A FileStream will allow you to seek to the portion of the file you want, no problem. It's the recommended way to do it in C#, and it's fast.

    Sharing between threads: You will need to create a lock to prevent other threads from changing the FileStream position while you're trying to read from it. The simplest way to do this:

    //  This really needs to be a member-level variable;
    private static readonly object fsLock = new object();
    
    //  Instantiate this in a static constructor or initialize() method
    private static FileStream fs = new FileStream("myFile.txt", FileMode.Open);
    
    
    public string ReadFile(int fileOffset) {
    
        byte[] buffer = new byte[bufferSize];
    
        int arrayOffset = 0;
    
        lock (fsLock) {
            fs.Seek(fileOffset, SeekOrigin.Begin);
    
            int numBytesRead = fs.Read(bytes, arrayOffset , bufferSize);
    
            //  Typically used if you're in a loop, reading blocks at a time
            arrayOffset += numBytesRead;
        }
    
        // Do what you want to the byte array and return it
    
    }
    

    Add try..catch statements and other code as necessary. Everywhere you access this FileStream, put a lock on the member-level variable fsLock... this will keep other methods from reading/manipulating the file pointer while you're trying to read.

    Speed-wise, I think you'll find you're limited by disk access speeds, not code.

    You'll have to think through all the issues about multi-threaded file access... who intializes/opens the file, who closes it, etc. There's a lot of ground to cover.

    0 讨论(0)
  • 2020-12-19 17:10

    If you are using .Net 4 look into using memory mapped files in the System.IO.MemoryMappedFiles namespace.

    They are perfect for reading small chunks out of large files. There are samples in the MSDN documentation.

    You can also do this in earlier versions of .Net, but then you need to wrap the Win32 API (or use http://winterdom.com/dev/net),

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