简体   繁体   English

在c#中将字符串转换为文件流

[英]Convert string to filestream in c#

Just started with writing unit tests and I am now, blocked with this situation: 刚开始写单元测试,我现在,被这种情况阻止:

I have a method which has a FileStream object and I am trying to pass a "string" to it. 我有一个方法,它有一个FileStream对象,我试图传递一个“字符串”。 So, I would like to convert my string to FileStream and I am doing this: 所以,我想将我的字符串转换为FileStream,我这样做:

File.WriteAllText(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),   
 @"/test.txt"), testFileContent); //writes my string to a temp file!


new FileStream(string.Concat(Environment.ExpandEnvironmentVariables("%temp%"),  
    @"/test.txt"), FileMode.Open) //open that temp file and uses it as a fileStream!

close the file then! 然后关闭文件!

But, I guess there must be some very simple alternative to convert a string to a fileStream. 但是,我想必须有一些非常简单的替代方法将字符串转换为fileStream。

Suggestions are welcome! 欢迎提出建议! [Note there are other answers to this question in stackoverflow but none seems to be a straight forward solution to that] [注意在stackoverflow中有这个问题的其他答案,但似乎没有一个直接的解决方案]

Thanks in advance! 提前致谢!

First of all change your method to allow Stream instead of FileStream . 首先更改您的方法以允许Stream而不是FileStream FileStream is an implementation which, as I remember, does not add any methods or properties, just implement abstract class Stream . FileStream是一个实现,我记得,它不添加任何方法或属性,只需实现抽象类Stream And then using below code you can convert string to Stream : 然后使用下面的代码,您可以将string转换为Stream

public Stream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

As FileStream class provides a stream for a file and hence it's constructor requires the path of the file,mode, permission parameter etc. to read the file into stream and hence it is used to read the text from file into stream. 由于FileStream类为文件提供了流,因此它的构造函数需要文件,模式,权限参数等的路径来将文件读入流中,因此它用于将文本从文件读取到流中。 If we need to convert string to stream first we need to convert string to bytes array as stream is a sequence of bytes. 如果我们需要首先将字符串转换为流,我们需要将字符串转换为字节数组,因为stream是一个字节序列。 Below is the code. 下面是代码。

   //Stream is a base class it holds the reference of MemoryStream
            Stream stream = new MemoryStream();
            String strText = "This is a String that needs to beconvert in stream";
             byte[] byteArray = Encoding.UTF8.GetBytes(strText);
             stream.Write(byteArray, 0, byteArray.Length);
             //set the position at the beginning.
             stream.Position = 0;
             using (StreamReader sr = new StreamReader(stream))
                        {
                           string strData;
                           while ((strData= sr.ReadLine()) != null)
                            {
                                Console.WriteLine(strData);
                            }
                        }

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

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