BinaryWriter和streamWriter之间有什么区别 [英] What is the difference between a BinaryWriter and streamWriter

查看:111
本文介绍了BinaryWriter和streamWriter之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我想知道binaryWriter和StreamWriter之间的区别。什么时候使用它是理想的?似乎两种方法都可以用于创建自定义文件扩展名,如.xyz,.doc等。在稳健性(可靠性),读写访问速度,更大的文件大小,数据加密等方面哪一种更好。知道使用stremWriter我可以创建隐藏的,只读的或系统文件,BinaryWriter可以吗?什么是序列化?有些文件根本无法删除,比如windows系统文件,我可以用其中任何一个创建吗?

Hi guys I would to know the difference between a binaryWriter and StreamWriter. When is it ideal to use one? It seems as if both methods can be used to create custom file extensions like .xyz,.doc etc. Which one is better in terms of robustness(reliability), read-write access speed, larger file size, data encryption, etc. I know with stremWriter i can create hidden,read-only or system files, is it possible with BinaryWriter? What is serialization anyway? There are files that cannot be deleted at all like windows system files, can i create one with either one of these?

推荐答案

1.StreamWriter更适用于文本和BinaryWriter用于原始类型,包括特定编码的字符串。 BinaryWriter写入整数的内存中二进制表示。 StreamWriter写入ASCII表示。一般来说,前者可以更紧凑和高效(考虑写整数23861398 - BinaryWriter需要4个字节,但StreamWriter需要8,16甚至32,具体取决于编码)但后者导致普通旧文本。

2.关于文件模式,这与使用的编写器无关,并且这两个类都有构造函数,需要一个可以作为文件的Stream,并且该文件可以使用不同的选项打开。 br />


1.StreamWriter is more for text and BinaryWriter is for primitive types including strings of particular encodings. BinaryWriter writes the in-memory binary representation of the integer. The StreamWriter writes the ASCII representation. Generally speaking, the former can be more compact and efficient (consider writing the integer 23861398 - the BinaryWriter would require 4 bytes, but the StreamWriter would require 8, 16, or even 32 depending on the encoding) but the latter results in plain old text.
2.Regarding the files mode, this is not related with the used writer, and both classes have constructor that require a Stream that could be a file and that file could be open with different options.

using (FileStream fs = new FileStream(fileName, FileMode.CreateNew))
          {
              using (StreamWriter writer = new StreamWriter(fs))
//you could uncomment the line bellow and comment the line above and should work!
              //using (BinaryWriter writer = new BinaryWriter (fs))
              {
                  writer.Write(textToAdd);
              }
          }





3.关于Serilization,这是一个很好的链接: MSDN链接 [ ^ ]

4.如果您的应用程序使用的用户拥有权限,您可以使用ReadOnly和/或Hidden属性创建文件将其放入指定的文件夹中。例如,要创建隐藏文件,您应该使用:



3. Regarding the Serilization here is a good link: MSDN link[^]
4. You can create file with ReadOnly and/or Hidden attributes if the user used by your application has rights to do it into a specified folder. For example to create a Hidden file you should use:

FileStream fs = new FileStream(Environment.CurrentDirectory + @"\Files\File.txt", FileMode.Create, FileAttributes.Hidden);


这篇关于BinaryWriter和streamWriter之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆