C# StreamWriter对像
用FileWriter来随机读取文件是个好主意,而用StreamWriter可以直接把字符串写入文件中,它处理重要的转换和向FileStream对像写入工作。创建StreamWriter有很多方法:
FileStream aFile = new FileStream(“Log.txt”,FileMode.CreatcNew);
StreamWriter sw = new StreamWriter(aFile);
也可直接从文件中创建StreamWriter对象:
StreamWriter sw = new StreamWriter(“Log.txt”,true);
后面这个Boolean值规定是附加文件还是创建新文件,如果此值为false,则就创建一个新文件,或者截取现有文件并打开它。如果此值设置为true,则打开文件,保留原来的数据,如果找不到文件,则创建一个新文件。注意:当创建FileStream对象时,您无法得到选项的范围。除了使用Boolean值附加或创建新文件外,我们根本就无法像使用FileStream类时那样规定FileMode属性;而且,您也无法设置FileAccess属性.因此您总是具有对文件的读写特权,为了使用任何高级参数,您必须在FileStream构造函数中规定这些参数,然后在FileStream对象中创建StreamWriter。
例:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace StreamWriterFile
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream aFile = new FileStream("data1.txt", FileMode.OpenOrCreate);//建立一个fileStream对象
StreamWriter sw = new StreamWriter(aFile);//用FileStream对像实例一个StreamWriter对象
sw.Write("first.");
sw.WriteLine("hello world!!!");//写入字符串,方法WriteLine写入时后面跟一个换行符
sw.Write("This is a");//写入字符串,方法Write写入时没有换行符
sw.Write("string of characters.");
sw.Close();//用完后必须关闭对像
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
}
}
}
C# StreamWriter对像的更多相关文章
- FileStream读写文件【StreamWriter 和 StreamReader】
FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能.这是因为FileStream类 ...
- C#中中文编码的问题(StreamWriter和StreamReader默认编码)
在使用StreamWriter和StreamReader时产生了这样的疑问,在不指定的情况下,他们使用什么编码方式? 查看MSDN,请看下图: 注意红色区域 这让我以为构造函数参数不同时使用不一样的 ...
- C#对文件/目录的操作:Path、File、Directory、FileStream、StreamReader、StreamWriter等类的浅析
以下类的命名空间都是:System.I/0; 一.Path:主要对文件路径的操作! 常用方法: String path=@"C:\a\b\c\123.txt"; 1-1.Path. ...
- C#--几个数据流Stream;StreamReader;StreamWriter;MemoryStream;BufferStream;
命名空间:System.IO; Stream: 各种流的基类,不能时行查找操作,Position属性不能修改.读取时不Position不会自动移动, HttpWebRequest webreq = ( ...
- C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)
(FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding) C#文 ...
- 文件流StreamReader和StreamWriter的使用
using (StreamReader sr = new StreamReader(@"C:\Users\shuai\Desktop\文件流读取.txt", Encoding.De ...
- (整理)streamWriter、streamReader与FileStream
今天偶然使用VS代码分析,发现CA2000警告,然后其中一条为streamWriter.streamReader与FileStream相关内容,特查询并记录一下. 引文地址:http://bbs.cs ...
- 关闭窗体后,利用StreamWriter保存控件里面的数据
以保存DataGridView里面的数据为例: 通过窗体增加的数据,没有用数据库保存,可以使用StreamWriter将数据存在临时文件里面,再次打开窗体时写入即可. private void For ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- StreamWriter和StremReader简单的用法
string str = "中国";//写入的内容 string path = @"e:\1.txt";//文件路径 StreamWriter sw = new ...
随机推荐
- oracle基本命令
1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...
- C# JObject将json字符串转为json对象
static void Main(string[] args) { string json = "{\"name\": \"测试\",\"m ...
- noip2018复习计划啊
需要复习的算法额: exgcd CRT INV dij spfa(~) 矩阵快速幂~高斯消元 tarjan(scc,bcc) treap splay 线段树 dp(决策单调,斜率,四边形不等式) rh ...
- PHP学习 文件操作函数的应用--简单网络留言模板
<?php /** 网络留言板模式 主要运用到的函数有 fopen fclose flock fwrite fread explode list */ ?> <meta http-e ...
- 1093 Count PAT's(25 分)
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...
- web应用,http协议简介,web框架
一.web应用 web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...
- mycat1.6.5分片(字符串拆分hash)
https://blog.csdn.net/webnum/article/details/78313525 分片规则:字符串拆分hash 一.conf/schema.xml文件 <?xm ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- (转)Linux命令学习总结:dos2unix - unix2dos
Linux命令学习总结:dos2unix - unix2dos 命令简介: 原文:http://www.cnblogs.com/kerrycode/p/5077969.html dos2unix是将W ...
- kill 与 kill -9(面试中问道的知识点)
转载自:http://www.2cto.com/os/201305/215267.html 需要特别说明的是,SIGKILL和SIGSTOP这两个信号既不能被应用程序捕获,也不能被操作系统阻塞或忽略. ...