File、FileStream、StreamWriter、StringWriter文件使用总结
一、File
1、File为静态类
File类,是一个静态类,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件。File类方法的参量很多时候都是路径path。主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。
常用的方法

二、FileStream
FileStream文件流 只能处理原始字节(raw byte)。FileStream 类可以用于任何数据文件,而不仅仅是文本文件。FileStream 对象可以用于读取诸如图像和声音的文件,FileStream读取出来的是字节数组,然后通过编码转换将字节数组转换成字符串。
string str = "今天天气好晴朗,处处好风光";
byte[] buttf = Encoding.Default.GetBytes(str);
//文件流的写入
using (FileStream fscreat = new FileStream(path, FileMode.Append, FileAccess.Write))
{
fscreat.Write(buttf, , buttf.Length);
}
三、StreamWriter
StreamWriter 类主要用于向流中写入数据
| 属性或方法 | 作用 |
|---|---|
| bool AutoFlush | 属性,获取或设置是否自动刷新缓冲区 |
| Encoding Encoding | 只读属性,获取当前流中的编码方式 |
| void Close() | 关闭流 |
| void Flush() | 刷新缓冲区 |
| void Write(char value) | 将字符写入流中 |
| void WriteLine(char value) | 将字符换行写入流中 |
| Task WriteAsync(char value) | 将字符异步写入流中 |
| Task WriteLineAsync(char value) | 将字符异步换行写入流中 |
class Program
{
static void Main(string[] args)
{
string path = @"D:\\code\\test.txt";
//创建StreamWriter 类的实例
StreamWriter streamWriter = new StreamWriter(path);
//向文件中写入姓名
streamWriter.WriteLine("小张");
//向文件中写入手机号
streamWriter.WriteLine("");
//刷新缓存
streamWriter.Flush();
//关闭流
streamWriter.Close();
}
}
四、StringWriter
实现用于将信息写入字符串的 TextWriter。 信息存储在基础 StringBuilder 中。
using System;
using System.IO; class StringRW
{
static void Main()
{
string textReaderText = "TextReader is the abstract base " +
"class of StreamReader and StringReader, which read " +
"characters from streams and strings, respectively.\n\n" + "Create an instance of TextReader to open a text file " +
"for reading a specified range of characters, or to " +
"create a reader based on an existing stream.\n\n" + "You can also use an instance of TextReader to read " +
"text from a custom backing store using the same " +
"APIs you would use for a string or a stream.\n\n"; Console.WriteLine("Original text:\n\n{0}", textReaderText); // From textReaderText, create a continuous paragraph
// with two spaces between each sentence.
string aLine, aParagraph = null;
StringReader strReader = new StringReader(textReaderText);
while(true)
{
aLine = strReader.ReadLine();
if(aLine != null)
{
aParagraph = aParagraph + aLine + " ";
}
else
{
aParagraph = aParagraph + "\n";
break;
}
}
Console.WriteLine("Modified text:\n\n{0}", aParagraph); // Re-create textReaderText from aParagraph.
int intCharacter;
char convertedCharacter;
StringWriter strWriter = new StringWriter();
strReader = new StringReader(aParagraph);
while(true)
{
intCharacter = strReader.Read(); // Check for the end of the string
// before converting to a character.
if(intCharacter == -) break; convertedCharacter = Convert.ToChar(intCharacter);
if(convertedCharacter == '.')
{
strWriter.Write(".\n\n"); // Bypass the spaces between sentences.
strReader.Read();
strReader.Read();
}
else
{
strWriter.Write(convertedCharacter);
}
}
Console.WriteLine("\nOriginal text:\n\n{0}",
strWriter.ToString());
}
}
File、FileStream、StreamWriter、StringWriter文件使用总结的更多相关文章
- File FileStream StreamWriter StreamReader文件读写操作方法
string path = "D:\\AccountChecking\\Test.txt"; string content = "abcdefg\r\nhigklmn\r ...
- File FileStream StreamReader StreamWriter C#
存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO ...
- 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)
1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...
- 【File】递归删除文件夹中子级文件/夹,并删除文件夹
今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...
- Filestream复制视频文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Java文件File操作一:文件的创建和删除
一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...
- java File delete 无法删除文件的原因。
windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...
- 如何用一张图片代替 'input:file' 上传本地文件??
今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的: 如何用一张图片代替 'input:file' 上传 ...
- 关于Java里面File类创建txt文件重复???
private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...
- Linux:file命令显示自定义文件类型
file 命令可以查看文件类型信息,原理见: 非常Linux-file命令与magic file 修改 /ect/magic 文件后,可用 file 命令显示自定义文件类型信息. man magic ...
随机推荐
- 灵魂画师,在线科普多云平台/CMP云管平台/中间件/虚拟化/容器是个啥
原创: 灵魂工作室 速石科技 经常碰到有人问: 你们是云管吗? 你们和CMP多云管理平台有什么区别? 你们这个多云平台到底是个啥? emmmmm,问题还挺不好回答. 为了说清楚这些问题,但又不希望你们 ...
- stars-one原创工具——m3u8视频下载合并器(kotlin)
一款可以下载m3u8.解密ts文件及合并ts文件的视频下载工具 蓝奏云下载地址 github地址 软件对你有帮助的话,不妨赞赏一波!感谢! 程序说明 采用多线程下载,可有效的提高下载速度 内置解密程序 ...
- cogs 186. [USACO Oct08] 牧场旅行 树链剖分 LCA
186. [USACO Oct08] 牧场旅行 ★★☆ 输入文件:pwalk.in 输出文件:pwalk.out 逐字节对比时间限制:1 s 内存限制:128 MB n个被自然地编号为 ...
- NTT - 牛客
链接:https://www.nowcoder.com/acm/contest/133/D来源:牛客网 题目描述 Applese打开了m个QQ群,向群友们发出了组队的邀请.作为网红选手,Applese ...
- Android布局属性与常用控件
一.Android常用布局属性 1. LinearLayout的特有属性 android:orientation:设置布局排列方式 android:layout_weight:设置所占布局的权重 ...
- Kubernetes concepts 系列
kubernetes concepts overview Pod overview Replication Controller Pod Liftcycle Termination Of Pod Re ...
- python小知识点总结
小知识点总结 1.python2和python3的区别 python2 python3 默认编码 ascii utf-8 input() raw_input() input() print 可以不 ...
- python接口自动化测试 - configparser配置文件解析器详细使用
configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...
- Day10-Python3基础-协程、异步IO、redis缓存、rabbitMQ队列
内容目录: Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko S ...
- PyCharm安装和使用教程(Windows系统)
说明: PyCharm 是一款功能强大的 Python 编辑器, 本文简单的介绍下PyCharm 在 Windows下是如何安装的. PyCharm 的下载地址:http://www.jetbrain ...