StreamWriter】的更多相关文章

FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能.这是因为FileStream类操作的是字节和字节数组,而Stream类操作的是字符数据.字符数据易于使用,但是有些操作,比如随机文件访问(访问文件中间某点的数据),就必须由FileStream对象执行.其中创建FileStream对象最简单的构造函数如下: 1        FileStream file = new FileS…
在使用StreamWriter和StreamReader时产生了这样的疑问,在不指定的情况下,他们使用什么编码方式? 查看MSDN,请看下图: 注意红色区域  这让我以为构造函数参数不同时使用不一样的字符,一种是utf-8,一种是默认编码. 然后我去测试.结果发现在使用StreamWriter时不指定字符集和指定字符集为默认两种结果是不一样的.惊讶吗! 每当我自己的想法和权威冲突时,一般的结果是我犯二了,这次也一样. 祭出绝招,使用WinHex查看写出来的文件,结果发现: 不指定的情况下为utf…
以下类的命名空间都是:System.I/0; 一.Path:主要对文件路径的操作! 常用方法: String path=@"C:\a\b\c\123.txt"; 1-1.Path.GetFileName(path); //获得文件名:123.txt 1-2.Path.GetExtenSion(path); //获得后缀名:.txt 1-3.Path.GetFileNameWithoutExtension(path); //获得无后缀文件名: 1-4.Path.GetDirectoryN…
命名空间:System.IO; Stream: 各种流的基类,不能时行查找操作,Position属性不能修改.读取时不Position不会自动移动, HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(new Uri("http://www.baidu.com/"));             Stream stream = ((HttpWebResponse)webreq.GetResponse()).GetRespon…
(FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding)     C#文件与流(FileStream.StreamWriter .StreamReader .File.FileInfo.Di,有需要的朋友可以参考下. 文件与流(FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.Directory…
using (StreamReader sr = new StreamReader(@"C:\Users\shuai\Desktop\文件流读取.txt", Encoding.Default)) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } } Console.ReadLine(); using(StreamWriter sw=new StreamWriter(@"C:\Users\shuai\…
今天偶然使用VS代码分析,发现CA2000警告,然后其中一条为streamWriter.streamReader与FileStream相关内容,特查询并记录一下. 引文地址:http://bbs.csdn.net/topics/390313584: http://blog.csdn.net/jaychouliyu/article/details/6152256 实现代码 using System; using System.Collections.Generic; using System.Li…
以保存DataGridView里面的数据为例: 通过窗体增加的数据,没有用数据库保存,可以使用StreamWriter将数据存在临时文件里面,再次打开窗体时写入即可. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //需要将集合中的数据存储 using (StreamWriter sw=new StreamWriter("save.txt")) { foreach (var item in…
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@"D:\test.txt", FileMode.Open);//初始化文件流 byte[] array = new byte[fileStream.Length];//初始化字节数组,用来暂存读取到的字节 fileStream.Read(array, , array.Length);//读取流中…
string str = "中国";//写入的内容 string path = @"e:\1.txt";//文件路径 StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode); sw.Write(str); sw.Close(); Console.ReadKey(); string path1 = @"e:\1.txt";//文件路径 FileStream fsFile =…
string path = @"D:\a.txt"; System.IO.StreamWriter swOut = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default);swOut.WriteLine(strA);swOut.Flush();swOut.Close();…
大文件拷贝: /// <summary> /// 大文件拷贝 /// </summary> /// <param name="sSource"></param> /// <param name="sTarget"></param> private static void CopyFile(string sSource, string sTarget) { using (FileStream fs…
public StreamWriter( string path, bool append ) 参数 path 类型:System.String要写入的完整文件路径. append 类型:System.Boolean若要追加数据到该文件中,则为 true:若要覆盖该文件,则为 false. 如果指定的文件不存在,该参数无效,且构造函数将创建一个新文件. …
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace _06职工工资文件翻倍练习 { class Program { static void Main(string[] args) { //1.读取salary1.txt using (StreamReader sr = new StreamReader("salary1.tx…
namespace filetest { class FileUtil { public static void WriteFile(string file) { using (FileStream fs = new FileStream(file,FileMode.Append)) using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("Hello,this is a StreamWriter test!"); }…
StreamReader实现了抽象基类TextReader类,而StreamWriter实现了抽象基类TextWriter.分别用于对流的读取与写入. 先从StreamReader说起 一.构造方法 StreamReader(Stream) 为指定的流初始化 StreamReader 类的新实例. StreamReader(String) 为指定的文件名初始化 StreamReader 类的新实例. StreamReader(Stream, Boolean) 用指定的字节顺序标记检测选项,为指定…
winform 通过webservice向服务器提交图片需要注意的地方 最近一个winform项目中需要通过拍照或者上传本地文件或者截图的方式把产品图片上传到服务器,最后选择了服务器部署webservice的方式来进行.其中遇到了一些问题记录下来. 不多说,直接上服务端代码 ? [WebMethod(Description = "上传文件")]      public bool UploadFiles(string filename, byte[] content)    {     …
string path = "D:\\AccountChecking\\Test.txt"; string content = "abcdefg\r\nhigklmn\r\nopqrst"; //操作文件夹对象,无则创建文件夹,可以一次创建多级 Directory.CreateDirectory("D:\\AccountChecking\\"); #region File-对于文件的典型操作,如复制.移动.重命名.创建.打开.删除和一次将追加到单…
/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ########## #### #### ########### #### #### ########### ##### ##### ### ######## ##### ##### ### ######## ###### ###### ### ########### ###### ###### #### ###…
经常用到,但老记不住,备忘一下 using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); sw.WriteLine("Hello World"); sw.Flush(); ms.Position = ; var sr = new StreamReader(ms); var myStr = sr.ReadToEnd(); Console.WriteLine(myStr); }…
StreamReader/StreamWriter操作的是字符数据(char),而FileStream操作的是字节数据(byte) FileStream与StreamXXXX类的默认编码都是UTF8,而一个中文字符占2个字符,所以StreamXXXX类常用于文本的打开与保存,而FileStream则用于数据的传输. StreamReader sw = new StreamReader(Stream stream); StreamReader sw = new StreamReader(strin…
实现一个 TextWriter,使其以一种特定的编码向流中写入字符. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace StreamReadWrite { class Program { static void Main(string[] args) { // Get the directories currently o…
https://blog.csdn.net/ymnl_gsh/article/details/80723050 C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 StreamReader类增强了FileStream,它让我们在字符串级别上操作文件,但有的时候我们还是需要在字节级上操作文件,却又不是一个字节 一个字节的操作,通常是2个.4个或8个字节这样操作,这便有了BinaryWriter和BinaryReader类,它们…
StreamWriter  Flush 即可. FileStream Flush 无用.…
sql = "Select case when date ='' then '0'else CONVERT(varchar(100), date, 101) end as date,case when ad ='' then '0'else CONVERT(varchar(100), ad, 101) end as ad,event1,type1,code from EventData where ip='" + UserIP + "' order by code,event…
//StreamRead来读取一个文件 using (StreamReader sr = new StreamReader(@"C:\Users\enle\Desktop\新建文本文档.txt", Encoding.Default)) { while (!sr.EndOfStream)//循环的去读文件 { Console.WriteLine(sr.ReadLine()); } } Console.ReadKey();  StreamWriter写入一个文本文件 //StreamWri…
存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO设备,就有FileStream,MemoryStream. FileStream可以实现任意格式,任意大小文件的读取,但是它的方法只能读取字节,不能直接读取读字符串或读一行等等,操作偏底层. 各种流的专用读写类Reader和Writer,主要用于方便读写文本和字符串,这些类中不仅可以用来读取字节,也…
FileStream读取文件 , array.Length);//读取流中数据把它写到字节数组中file.Close();//关闭流string str =Encoding.Default.GetString(array);//将字节数组内容转化为字符串Console.WriteLine(str); FileStream写入文件 , array.Length);//将字节数组写入文件流file.Close();//关闭流 MemoryStream解析数据 string str ="Hi!你好!&…
StreamReader和StreamWriter操作字符的 FileStream操作字节的 //使用StreamReader读取文件 using (StreamReader sr=new StreamReader(@"D:\\1.txt",Encoding.UTF8)) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); //一行一行读 } } //使用StreamWriter写入文件 //true表示追加内容,…
原文:http://www.cnblogs.com/ybwang/archive/2010/06/12/1757409.html 参考: 1. <C#高级编程>第六版 2.  文件流和数据流-C#程序设计教程 2010-7-11补充: 发现了一篇讲编码的深入而全面的好文章http://www.cnblogs.com/KevinYang/archive/2010/06/18/1760597.html 向文件写入非字符类型数据 当向文件中写入非字符类型的数据时,StreamWriter和Binar…