Create and write to a text file inmemory and convert to byte array in one go

非 Y 不嫁゛ 提交于 2019-12-22 01:56:25

问题


How can I create a .csv file implicitly/automatically by using the correct method, add text to that file existing in memory and then convert to in memory data to a byte array?

string path = @"C:\test.txt";
File.WriteAllLines(path, GetLines());
byte[] bytes = System.IO.File.ReadAllBytes(path);

With that approach I create a file always (good), write into it (good) then close it (bad) then open the file again from a path and read it from the hard disc (bad)

How can I improve that?

UPDATE

One nearly good approach would be:

  using (var fs = new FileStream(@"C:\test.csv", FileMode.Create, FileAccess.ReadWrite))
            {

                using (var memoryStream = new MemoryStream())
                {

                    fs.CopyTo(memoryStream );
                    return memoryStream .ToArray();
                }

        }

but I am not able to write text into that filestream... just bytes...

UPDATE 2

 using (var fs = File.Create(@"C:\temp\test.csv"))
            {
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    using (var ms = new MemoryStream())
                    {
                        String message = "Message is the correct ääüö Pi(\u03a0), and Sigma (\u03a3).";
                        sw.Write(message);
                        sw.Flush();
                        fs.CopyTo(ms);
                        return ms.ToArray();
                    }
                }
            }

The string message is not persisted to the test.csv file. Anyone knows why?


回答1:


Write text into Memory Stream.

byte[] bytes = null;
 using (var ms = new MemoryStream())
 {
     TextWriter tw = new StreamWriter(ms);
     tw.Write("blabla");
     tw.Flush();
     ms.Position = 0;
     bytes = ms.ToArray();
     //or save to disk using FileStream (fs)
     ms.WriteTo(fs);
 }

UPDATE

Use file stream Directly

 using (var fs = new FileStream(@"C:\sh\test.csv", FileMode.Create, FileAccess.ReadWrite))
 {
     TextWriter tw = new StreamWriter(fs);
     tw.Write("blabla");
     tw.Flush();
 }



回答2:


You can get a byte array from a string using encoding:

Encoding.ASCII.GetBytes(aString);

Or

Encoding.UTF8.GetBytes(aString);

But I don't know why you would want a csv as bytes. You could load the entire file to a string, add to it and then save it:

string content;

using (var reader = new StreamReader(filename))
{
    content = reader.ReadToEnd();
}

content += "x,y,z";

using (var writer = new StreamWriter(filename))
{
    writer.Write(content);
}

Update: Create a csv in memory and pass back as bytes:

var stringBuilder = new StringBuilder();
foreach(var line in GetLines())
{
    stringBuilder.AppendLine(log);
}
return Encoding.ASCII.GetBytes(stringBuilder.ToString());


来源:https://stackoverflow.com/questions/36661211/create-and-write-to-a-text-file-inmemory-and-convert-to-byte-array-in-one-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!