c# - 删除文件前几个字节的最快方法

标签 c# file-io windows-mobile-6 windows-mobile-6.5

我正在使用 Windows Mobile 精简版 6.5 手机,并通过蓝牙将二进制数据写入文件。这些文件变得非常大,16M+,我需要做的是一旦文件被写入,我需要在文件中搜索起始字符,然后删除之前的所有内容,从而消除垃圾。当数据传入时,由于图形问题和速度,我无法内联执行此操作,因为我收到大量数据传入,并且传入数据上已经存在太多 if 条件。我认为最好是发布流程。无论如何,这是我的困境,搜索起始字节的速度和文件的重写有时需要 5 分钟或更长时间......我基本上将文件移动到临时文件中,通过它进行解析并重写一个全新的文件。我必须逐字节地执行此操作。

private void closeFiles() {
    try {

    // Close file stream for raw data.
    if (this.fsRaw != null) {
        this.fsRaw.Flush();
        this.fsRaw.Close();

        // Move file, seek the first sync bytes, 
        // write to fsRaw stream with sync byte and rest of data after it
        File.Move(this.s_fileNameRaw, this.s_fileNameRaw + ".old");
        FileStream fsRaw_Copy = File.Open(this.s_fileNameRaw + ".old", FileMode.Open);
        this.fsRaw = File.Create(this.s_fileNameRaw);

        int x = 0;
        bool syncFound = false;

        // search for sync byte algorithm
        while (x != -1) {
            ... logic to search for sync byte
            if (x != -1 && syncFound) {
                this.fsPatientRaw.WriteByte((byte)x);
            }
        }

        this.fsRaw.Close();

        fsRaw_Copy.Close();
        File.Delete(this.s_fileNameRaw + ".old");
    }


    } catch(IOException e) {
        CLogger.WriteLog(ELogLevel.ERROR,"Exception in writing: " + e.Message);
    }
}

一定有比这更快的方法!

------------使用答案测试时间-------------

通过一字节读取和一字节写入初步测试我的方式:

27 Kb/sec

使用下面的答案和 32768 字节缓冲区:

321 Kb/sec

使用下面的答案和 65536 字节缓冲区:

501 Kb/sec

最佳答案

您正在对整个文件进行按字节的复制。由于多种原因,这种做法效率不高。搜索起始偏移量(如果需要两者,则搜索结束偏移量),然后将两个偏移量(或起始偏移量和文件结尾)之间的整个内容从一个流复制到另一个流。

编辑

您不必阅读全部内容即可进行复制。像这样的东西(未经测试,但你明白了)会起作用。

private void CopyPartial(string sourceName, byte syncByte, string destName)
{
    using (var input = File.OpenRead(sourceName))
    using (var reader = new BinaryReader(input))
    using (var output = File.Create(destName))
    {
        var start = 0;
        // seek to sync byte
        while (reader.ReadByte() != syncByte)
        {
            start++;
        }

        var buffer = new byte[4096]; // 4k page - adjust as you see fit

        do
        {
            var actual = reader.Read(buffer, 0, buffer.Length);
            output.Write(buffer, 0, actual);
        } while (reader.PeekChar() >= 0);
    }

}

编辑2

我今天实际上需要类似的东西,所以我决定在不调用 PeekChar() 的情况下编写它。这是我所做的核心 - 请随意将其与上面的第二个 do...while 循环集成。

            var buffer = new byte[1024];
            var total = 0;

            do
            {
                var actual = reader.Read(buffer, 0, buffer.Length);
                writer.Write(buffer, 0, actual);
                total += actual;
            } while (total < reader.BaseStream.Length);

关于c# - 删除文件前几个字节的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7367153/

相关文章:

c# - 当声音输入达到一定水平时开始使用 naudio 录制波形

c# - 我应该在 Xamarin.Forms 上使用哪些 SQLite (sqlite-net-pcl) 标志?

linux - 在本地复制大文件的最快方法

java - 如何用JAVA向文件中写入特定数据

c# - 如何使用 MsBuild 以编程方式停止或启动 IIS(6.0 和 7.0)中的网站?

c# - 如何使用附加参数更新 Linq 表达式?

java - 在Java中将文本文件读入char数组

windows-mobile-6 - 在windows mobile上待机时播放声音?

.net - 在 Windows Mobile 上打开/关闭 LED 灯和手电筒

.net - 没有足够的存储空间来完成此操作 - 程序或存储内存?