- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个由一系列对象组成的文件格式,其中每个对象都有一个格式如下的标题:
public struct FileObjectHeader {
//The type of the object (not important for this question, but it exists)
public byte TypeID;
//The length of the object's data, which DOES NOT include the size of the header.
public UInt16 Length;
}
后跟指定长度的数据。
我通过首先为每个对象和对象的标题创建位置列表来读取此数据:
struct FileObjectIndex {
public FileObjectHeader Header;
public long Location;
}
public List<FileObject> ReadObjects(Stream s) {
List<FileObjectReference> objectRefs = new List<FileObjectReference>();
try {
while (true) {
FileObjectHeader header = ReadObjectHeader(s);
//The above advances the stream by the size of the header as well.
FileObjectReference reference = new FileObjectReference() { Header = header, Position = stream.Position };
objectRefs.add(reference);
//Advance the stream to the next object's header.
s.Seek(header.Length, SeekOrigin.Current);
}
} catch (EndOfStreamException) {
//Do nothing as this is an expected case
}
//Now we'd read all of the objects that we've previously located.
//This code isn't too important for the question but I'm including it for reference.
List<FileObject> objects = new List<FileObject>();
foreach (var reference in objectRefs) {
s.seek(reference.Location, SeekOrigin.Begin);
objects.add(ReadObject(reference.Header, s));
}
return objects;
}
一些注意事项:
FileStream
,但我也不能保证。但是,对于这种情况,我主要担心 FileStreams。我的问题是:
因为我正在使用 FileStream.seek
,是否会使用 seek 导致超出流末尾并无限期扩展文件的情况?根据文档:
You can seek to any location beyond the length of the stream. When you seek beyond the length of the file, the file size grows. In Windows NT and later versions, data added to the end of the file is set to zero. In Windows 98 or earlier versions, data added to the end of the file is not set to zero, which means that previously deleted data is visible to the stream.
按照上述方式,它似乎可以在我不扩展到的情况下扩展文件,导致文件不断增长,因为它从 header 读取 3 个字节。实际上,这似乎不会发生,但我想确认它不会发生。
最佳答案
FileStream.Read()
的文档然而说:
Return Value
Type: System.Int32
The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.
因此我强烈怀疑(但你应该自己验证这一点)这种追尾只适用于你之后写入文件的情况。这是有道理的 - 如果您知道自己需要它,您可以预留空间,而无需在其中实际写入任何内容(这会很慢)。
然而,当读取时,我的猜测是您应该返回 0
并且不会读取任何数据。此外,没有文件扩展。
关于c# - 以这种方式使用 FileStream.seek 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860951/
我需要对同一文件进行一批写入,但在文件内的不同位置。我想以尽可能最好的性能实现这一目标,因此查看了同步 FileStream.Write 和异步 FileStream.BeginWrite 方法。 同
MSDN说FileStream.Flush(True) “还清除所有中间文件缓冲区。”。 “所有中间文件缓冲区”到底是什么意思? 最佳答案 它会将缓冲在文件系统缓存中的文件数据写入磁盘。该数据通常是根
考虑以下摘自 Microsoft docs 的代码: using FileStream createStream = File.Create(fileName); // ...write to str
我对Spark的理解fileStream()方法是将三种类型作为参数:Key , Value , 和 Format .对于文本文件,适当的类型是:LongWritable , Text , 和 Tex
为什么 FileStream.Length 是 long 类型,但 FileStream.Read 参数 - offset 有更短的长度 int 呢? 布莱恩 最佳答案 offset 参数告诉从哪里开
我编写了以下程序,其目的是创建一个给定大小的文件,其中包含一些随机数据。该程序运行良好,并完成了它应该做的事情。但是,我不明白为什么它会消耗 5GB 的 RAM(请参阅我的任务管理器的屏幕截图)。当我
我在一次采访中被问到这个问题,我说答案是 Managed。面试官似乎很惊讶。我的问题是即使它访问一个文件( native /非托管资源),但这个类不是托管的吗?或者你认为我应该有一些后续问题以获得更多
我正在编写一些代码作为打开文件框架的一部分。该文件属于自定义类型,不应由我的应用程序的多个实例打开。为了停止打开多个文件,我使用文件流创建一个锁定文件,然后保持所述文件流打开。 这似乎可以防止我的应用
我正在使用 Apache Commons Net 的 FTPClient 从位于服务器上的文件中读取内容。仅读取一次时效果很好。但是当我尝试读取第二个文件时,FTPClient 的 InputStre
问题 有没有办法在 C# 中创建带偏移量的 FileStream?例如,如果我在偏移量 100 处打开 SomeFile.bin,Stream.Position 将等于 0,但读取和写入将偏移 100
我正在阅读一个简单的文本文件,其中包含使用文件流类的单行。但似乎 filestream.read 在开头添加了一些垃圾字符。 代码下方。 using (var _fs = File.Open(_idF
我正在使用 FileStream 将 FTP 服务器的信息下载到我的 C:\驱动器上的目录中。出于某种原因,即使我什至尝试将目录权限设置为“所有人”访问权限,它也给了我这个异常(exception):
我正在尝试通过将文件作为参数的 API 上传 .srt 文件。 文件存储在服务器上,我正在使用 FileStream 和 StreamWriter 写入: string path = Server.M
我四处搜索了一下,但找不到能完美解决我的问题的东西。我有一些代码,即来 self 的数据库的 FileStream varbinary,并将其制作成客户端计算机上的文件,双击时可以在文件类型的默认应用
我最近在做一个涉及很多FileStreaming 的项目,这是我以前没有真正接触过的。 为了尝试更好地熟悉这些方法的原理,我编写了一些代码(理论上)将文件从一个 dir 下载到另一个,并逐步完成,在我
我通过例如下载文件5 个线程。当其中一个线程完成下载文件部分时 - 它被中止,但所有其余线程都有 ThreadState = WaitSleepJoin 并且显然停止下载。如何解决? while ((
我试图将 5 GB 的 ISO 文件复制到具有 29 GB 可用空间的 32 GB 闪存驱动器上。 Windows 7 拒绝让我拖放文件到闪存驱动器,报告文件对于目标文件系统来说太大了。 我最终了解到
我发现将 BufferedStream 与 FileStream 结合使用没有意义,因为它有自己的缓冲策略。然而,我想知道一件事: FileStream fsWithBuffer = new File
我有一个只读的 FileStream,它是一个方法局部变量: public void SomeMethod() { var fileStream = File.Open(fileName, Fi
我有两个文件流,它们从不同的文件中收集不同的信息: FileStream dataStruc = new FileStream("c:\\temp\\dataStruc.txt", FileMode.
我是一名优秀的程序员,十分优秀!