简体   繁体   English

使用FileStream直接读取字节文档(无路径)

[英]Directly read a byte document with FileStream (without path)

I have a byte document in a variable, and I want to put it into a FileStream for using it into a StreamReader . 我在变量中有一个字节文档,我想将其放入FileStream以便在StreamReader使用它。

Can I do this? 我可以这样做吗?

I saw that FileStream uses a path, but is there a way to read my byte document? 我看到FileStream使用路径,但是有没有办法读取我的字节文档?

I got something like this, of course, the myByteDocument doesn't work, because it's not a path: 我得到了这样的东西,当然, myByteDocument不起作用,因为它不是路径:

file = new FileStream(myByteDocument, FileMode.Open, FileAccess.Read, FileShare.Read);

reader= new StreamReader(file); 
reader.BaseStream.Seek(0, SeekOrigin.Begin);
string fullText= "";
while (reader.Peek() > -1) 
{
    fullText+= reader.ReadLine();
}
reader.Close();

myByteDocument is obtain like this : myByteDocument是这样获得的:

DataRow row = vDs.Tables[0].Rows
byte[] myByteDocument = (byte[])row.ItemArray[0];

I read the document, and put it into a string to replace, some pieces of it, and then after all the replace, I create a new document with the fullText variable, with something like sw.write(fullText) , where sw is a StreamWriter . 我阅读了文档,然后将其放入字符串中进行替换,然后替换掉所有部分,然后创建一个带有fullText变量的新文档,其中包含sw.write(fullText) ,其中sw是一个StreamWriter

So, I want to read the file, without knowing the path, but with the byte document directly. 因此,我想在不知道路径的情况下读取文件,而是直接使用字节文档。 Can I do this? 我可以这样做吗?

If I'm not clear, don't hesitate, say it. 如果我不清楚,请不要犹豫。

You should look at the MemoryStream class instead of FileStream . 您应该查看MemoryStream类而不是FileStream It should provide the functionality you need to read the file without knowing the path (provided myByteDocument is a byte array). 它应该提供在不知道路径的情况下读取文件所需的功能(假设myByteDocument是字节数组)。

var file = new MemoryStream(myByteDocument);

You can basically use your same code just substituting the MemoryStream constructor for the FileStream constructor you were trying to use. 您基本上可以使用相同的代码,只需将MemoryStream构造函数替换为您尝试使用的FileStream构造函数。

string fullText= "";

using(var file = new MemoryStream(myByteDocument))
using(var reader = new StreamReader(file))
{
    reader.BaseStream.Seek(0, SeekOrigin.Begin);
    while (!reader.EndOfStream)
    {
        fullText += reader.ReadLine();
    }
}

Please note, I also added using blocks for the file access. 请注意,我还添加了using块用于文件访问。 This is much preferable to just calling reader.Close() as it will ensure the cleanup of the resources even if an Exception occurs where just calling the Close() method will not. 这比只调用reader.Close()更可取,因为即使在没有调用Close()方法的情况下发生异常,也可以确保清除资源。

What you need is convert your byte array to string, then replace what you need, and write the final result. 您需要的是将字节数组转换为字符串,然后替换所需的内容并写入最终结果。 You don't even need StreamReaders or writers to do this. 您甚至不需要StreamReaders或writer来执行此操作。

Take a look at this post: How to convert byte[] to string? 看一下这篇文章: 如何将byte []转换为字符串?

Here you have different methods to convert your document to a string. 在这里,您可以使用不同的方法将文档转换为字符串。 The one accepted as an answer is: 可以接受的答案是:

string result = System.Text.Encoding.UTF8.GetString(myByteDocument)

Once you've done all your replacements, just save the string into a file, with something like that (most simple way): 完成所有替换操作后,只需将字符串保存到文件中,即可(最简单的方法):

File.WriteAllText(path, result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM