我正在使用 Windows Mobile Compact Edition 6.5 手机并将二进制数据从蓝牙写入文件。这些文件变得非常大,16M+,我需要做的是,一旦文件被写入,我需要在文件中搜索开始字符,然后删除之前的所有内容,从而消除垃圾。由于图形问题和速度,当数据进入时,我无法内联执行此操作,因为我收到大量数据,并且传入数据的条件已经太多。我认为最好发布过程。无论如何,这是我的困境,搜索起始字节的速度和文件的重写有时需要 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