2011-12-30 96 views
27

有什麼辦法從文件中讀取特定字節?c# - 讀取文件的特定字節

例如,我有以下代碼來讀取文件

byte[] test = File.ReadAllBytes(file); 

我想讀偏離50字節偏移60,並把它們在陣列中的所有字節。

回答

12

LINQ版本:

byte[] test = File.ReadAllBytes(file).Skip(50).Take(10).ToArray(); 
+39

這裏將讀取所有文件內容,然後只使用10個字節。不是非常優化的方法:) – 2011-12-30 11:56:09

+1

@the_joric然而,給定文件名的助手返回一個懶惰的'IEnumerable '代替'File.ReadAllBytes'將是一種有效的方法,特別是如果從文件中讀取任意字節的運行是共同需要。 – Richard 2011-12-30 13:51:21

+7

這不應該被接受的答案 – neo112 2016-05-28 08:53:15

26

這應該這樣做

var data = new byte[10]; 
int actualRead; 

using (FileStream fs = new FileStream("c:\\MyFile.bin", FileMode.Open)) { 
    fs.Position = 50; 
    actualRead = 0; 
    do { 
     actualRead += fs.Read(data, actualRead, 10-actualRead); 
    } while (actualRead != 10 && fs.Position < fs.Length); 
} 

完成後,data將包含文件的之間的10個字節的50和60,和actualRead偏移將包含一個從0到10,表明了多少字節被實際閱讀(當文件至少有50個但少於60個字節時,這是很有意義的)。如果文件小於50個字節,您將看到EndOfStreamException

+1

你的意思是根據需要始終檢查Read和循環的返回值。即使在另外20000字節可用時,Read也返回1是合法的。 – 2011-12-30 11:36:50

+0

從MSDN上的FilStream.Read開始:「即使未達到流的末尾,實現也可以自由返回比請求更少的字節。」 – 2011-12-30 11:43:19

+0

重要的是:文檔明確保留的權利:所以 - 我你沒有,你沒有遵循公佈的API – 2011-12-30 11:50:07

1
using System.IO; 

public static byte[] ReadFile(string filePath) 
{ 
    byte[] buffer; 
    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
    try 
    { 
     buffer = new byte[length];   // create buffer 
     fileStream.Read(buffer, 50, 10); 
    } 
    finally 
    { 
     fileStream.Close(); 
    } 
    return buffer; 
} 
+2

調用Read的「偏移量」是緩衝區**中的偏移**,而不是流中的偏移量 – 2011-12-30 11:37:46

31

創建一個BinaryReader在讀10個字節開始字節50:

byte[] test = new byte[10]; 
using (BinaryReader reader = new BinaryReader(new FileStream(file, FileMode.Open))) 
{ 
    reader.BaseStream.Seek(50, SeekOrigin.Begin); 
    reader.Read(test, 0, 10); 
} 
+1

Stream.Seek方法有兩個參數。它應該是'reader.BaseStream.Seek(50,SeekOrigin.Begin);' – Tajomaru 2013-09-06 10:51:24

+0

好抓,固定。 – 2013-09-06 12:57:39

+1

最好,最簡單的答案!謝謝 – knocte 2014-01-13 19:34:58

-3
byte[] a = new byte[60]; 
byte[] b = new byte[10]; 
Array.Copy(a ,50, b , 0 , 10); 
+0

-1:Q是關於從*文件*中讀取字節的。 – Richard 2011-12-30 11:32:59

+0

只因爲你編輯了這個問題......在OP中,這個問題並不清楚。 – Adrian 2011-12-30 11:35:40

+0

我建議你看看我的編輯。文件要求在那裏(並且我沒有改變標題)。 – Richard 2011-12-30 11:37:21

1

您可以使用FILESTREAM來,然後調用read

string pathSource = @"c:\tests\source.txt"; 

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 = 10; 
    int numBytesRead = 50; 
    // Read may return anything from 0 to numBytesToRead. 
    int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); 
} 

Check this example MSDN

+0

numBytesRead是偏移量,參數變化(緩衝區,偏移量,計數) – oqx 2011-12-30 11:39:51

+0

你是對的,刪除投票。 – Richard 2011-12-30 11:41:34

+0

非常感謝:) – oqx 2011-12-30 11:42:45

2

你需要:

  • 求於你想要
  • 通話反覆閱讀,檢查返回值的數據,直到你把所有的數據,需要

例如:

public static byte[] ReadBytes(string path, int offset, int count) { 
    using(var file = File.OpenRead(path)) { 
     file.Position = offset; 
     offset = 0; 
     byte[] buffer = new byte[count]; 
     int read; 
     while(count > 0 && (read = file.Read(buffer, offset, count)) > 0) 
     { 
      offset += read; 
      count -= read; 
     } 
     if(count < 0) throw new EndOfStreamException(); 
     return buffer;  
    } 
}