File、FileStream、StreamWriter、StringWriter文件使用总结
2024-08-28 01:09:20
一、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());
}
}
最新文章
- 几款不错的VisualStudio2010插件
- Java并发包中CyclicBarrier的工作原理、使用示例
- Github学习之路-初出茅庐,接触Github,了解Github
- C语言指针与数组的定义与声明易错分析
- (五)u-boot2013.01.01 for TQ210:《移植前的准备及u-boot初编译》
- iOS通过UIAlertController弹出底部选择框来调用相机或者相册
- Java 程序员在写 SQL 时常犯的 10 个错误
- (三)Boost库之字符串处理
- 1.Hibernate框架核心组件 (转自冯岩)
- Java笔记:Java 流(Stream)、文件(File)和IO
- DatasourceUtils类:获取连接池和数据库连接
- 将多个图片合并到一个TIF文件里(非 GDAL) 优化版
- url文件的格式
- iOS之ProtocolBuffer搭建
- drupal 用法小结,drupal select ,query ,distinct
- hibernate-release-4.3.11.Final资源包介绍
- [转]Device Context 设备环境 设备上下文 理解
- 360杀毒导致的 VS 报扩展错误,请查看 ActiveLog.xml
- Yii GridView::widget
- 课时68.id选择器(掌握)
热门文章
- 通过例子进阶学习C++(五)计算2的1次方至2的64次方之和
- Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档
- npm 安装出现 run `npm audit fix` to fix them, or `npm audit` for details 解决办法
- 【Linux】---Linux系统下各种常用命令总结
- 点分治 (等级排) codeforces 321C
- React Context 的用法
- Redis(三):set/get 命令解析
- CSS动效集锦,视觉魔法的碰撞与融合(三)
- 什么是“跑面”呢? - ERSS耳斯百科:您的随身移动百科
- Linux系统实时数据同步inotify+rsync