C# 尝试在使用 MemoryStream 类时替换字节
我从大型机获取一个文本文件,有时文本行中间会注入一些 0x0D。
以前的程序员使用 FileStream 类创建了一个方法。此方法效果很好,但需要大约 30 分钟才能浏览整个文件。
我的想法是将所需的文本行(大约 25 行)传递给一种方法以减少处理时间。
我一直在使用 MemoryStream 类,但遇到问题,它找不到 0x0D
控制代码。
这是当前的 FileStream 方法:
private void ReplaceFileStream(string strInputFile)
{
FileStream fileStream = new FileStream(strInputFile, FileMode.Open, FileAccess.ReadWrite);
byte filebyte;
while (fileStream.Position < fileStream.Length)
{
filebyte = (byte)fileStream.ReadByte();
if (filebyte == 0x0D)
{
filebyte = 0x20;
fileStream.Position = fileStream.Position - 1;
fileStream.WriteByte(filebyte);
}
}
fileStream.Close();
}
这是 MemoryStream 方法:
private void ReplaceMemoryStream(string strInputLine)
{
byte[] byteArray = Encoding.ASCII.GetBytes(strInputLine);
MemoryStream fileStream = new MemoryStream(byteArray);
byte filebyte;
while (fileStream.Position < fileStream.Length)
{
filebyte = (byte)fileStream.ReadByte();
if (filebyte == 0x0D)
{
filebyte = 0x20;
fileStream.Position = fileStream.Position - 1;
fileStream.WriteByte(filebyte);
}
}
fileStream.Close();
}
因为我之前没有使用过 MemoryStream 类,所以对它不太熟悉。有什么建议或想法吗?
I get a text file from a mainframe and sometimes there are some 0x0D injected into the middle of the text lines.
The previos programmer created a method using the FileStream class. This method works fine but is taking around 30 minutes to go thru the entire file.
My thought was to pass the text lines that are needed (about 25 lines) to a method to decrease the processing time.
I've been working with the MemoryStream class but am having issue where it does not find the 0x0D
control code.
Here is the current FileStream method:
private void ReplaceFileStream(string strInputFile)
{
FileStream fileStream = new FileStream(strInputFile, FileMode.Open, FileAccess.ReadWrite);
byte filebyte;
while (fileStream.Position < fileStream.Length)
{
filebyte = (byte)fileStream.ReadByte();
if (filebyte == 0x0D)
{
filebyte = 0x20;
fileStream.Position = fileStream.Position - 1;
fileStream.WriteByte(filebyte);
}
}
fileStream.Close();
}
and here is the MemoryStream method:
private void ReplaceMemoryStream(string strInputLine)
{
byte[] byteArray = Encoding.ASCII.GetBytes(strInputLine);
MemoryStream fileStream = new MemoryStream(byteArray);
byte filebyte;
while (fileStream.Position < fileStream.Length)
{
filebyte = (byte)fileStream.ReadByte();
if (filebyte == 0x0D)
{
filebyte = 0x20;
fileStream.Position = fileStream.Position - 1;
fileStream.WriteByte(filebyte);
}
}
fileStream.Close();
}
As I have not used the MemoryStream class before am not that familar with it. Any tips or ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的替换代码在流中找不到
0x0D
,而之前使用 FileStream 的方法却找到了,我认为这可能是因为您用于获取文件字节的编码,您可以尝试使用其他一些编码类型。否则你的代码似乎没问题,我会在 MemoryStream 周围使用 using 来确保它被关闭和处置,如下所示:
查看你的代码,我不能 100% 确定你对内存流所做的更改将被持久化;实际上我认为如果更改后不保存,您的更改将会丢失。我可能是错的,但你应该测试一下,如果它没有保存,你应该在更改后使用 StreamWriter 来保存它。
if your replacement code does not find the
0x0D
in the stream and the previous method with the FileStream does it, I think it could be because of the Encoding you are using to get the bytes of the file, you can try with some other encoding types.otherwise your code seems to be fine, I would use a using around the MemoryStream to be sure it gets closed and disposed, something like this:
looking at your code I am not 100% sure the changes you make to the memory stream will be persisted; Actually I think that if you do not save it after the changes, your changes will be lost. I can be wrong in this but you should test and see, if it does not save you should use StreamWriter to save it after the changes.
我不知道你的文件的大小,但如果它们足够小,你可以一次将整个文件加载到内存中,那么你可以这样做:
如果你不能一次读取整个文件,那么您应该切换到缓冲读取类型的设置,这里是一个从文件读取,写入临时文件,然后最后将临时文件复制到原始文件的示例。与一次读取一个字节的文件相比,这应该会产生更好的性能:
I don't know the size of your files, but if they are small enough that you can load the whole thing in memory at once, then you could do something like this:
If you can't read the whole file in at once, then you should switch to a buffered reading type of setup, here is an example that reads from the file, writes to a temp file, then in the end copies the temp file over the original file. This should yield better performance then reading a file one byte at a time: