C#中流的使用和分类
作者:Darren?Ji 发布时间:2022-10-04 22:17:41
使用流读取、写入文件
使用流把文件读取到字节数组:
//FileMode.Create, FileMode.Append
//FileAccess.Write, FileAccess.ReadWrite
//FileMode和FileAccess搭配使用,如果第二个参数FileMode.Appden写追加,第三个参数FileAccess.Read只读,会抛异常
Stream source = new FileStream(@"1.jpg",FileMode.Open, FileAccess.Read)
byte[] buffer = new byte[source.Length];
int bytesRead = source.Read(buffer, i, (int)source.Length);
Int32类型的最大值,以及Byte, KB, MB, GB转换:
Int32.MaxValue = 2147483647 Byte
2147483647/1024 = 2097152 KB(1 KB = 1024 Byte)
2097152/1024 = 2048 MB(1 M = 1024 KB)
2048/1024 = 2 G(1G = 1024M)
使用流把字节数组写到文件:
Stream target = new FileStream(@"2.jpg", FileMode.Create, FileAccess.Write);
Stream source = new FileStream(@"1.jpg",FileMode.Open, FileAccess.Read)
byte[] buffer = new byte[source.Length];
int bytesRead = source.Read(buffer, i, (int)source.Length);
target.Write(buffer, 0, buffer.Length);
source.Dispose();
target.Dispose();
使用流对大文件进行分批读取和写入:
int BufferSize = 10240; // 10KB
Stream source = new FileStream(@"D:\a.mp3", FileMode.Open, FileAccess.Read);
Stream target = new FileStream(@"D:\b.mp3", FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[BufferSize];
int byteRead;
do{
byteRead = source.Read(buffer, 0, BufferSize);
target.Write(buffer, 0, bytesRead);
} while(byteRead > 0);
target.Dispose();
source.Dispose();
流的分类
在Stream抽象类下包含:
→FileStream→IsolatedStoreageFileStream
→MemoryStream
→NetworkStream
基础流
从流中读取数据:
CanRead()
Read(byte[] buffer, int offset, int count)
向流中写入数据:
CanWrite()
Write(byte[] buffer, int offset, int count)
WriteByte(Byte value)
移动流指针:
CanSeek()
Seek(long offset, SeekOrigion)
Position流的指针位置
Close()
Dispose()
Flush()将缓存设备写入存储设备
CanTimeout()
ReadTimeout()
WriteTimeout()
Length
SetLength(long value)
装饰器流
实现了Decorator模式,包含对Stream抽象基类的引用,同时继承自Stream抽象基类。
System.IO.Compression下的DeflateStream和GZipStream用于压缩和解压缩
System.Security.Cryptography下的CryptoStream用于加密和解密
System.Net.Security下的AuthenticatedStream用于安全性
System.IO下的BufferedStream用户缓存
包装器类
不是流类型,而是协助开发者处理流包含的数据,并且不需要将流读取到Byte[]字节数组中。但流的包装器类包含了对流的引用。
StreamReader
继承自TextReader。
将流中的数据读取为字符。
FileStream fs = new FileStream("a.txt", FileMode.Open, FileAcess.Read);
StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
//或者
//StreamReader reader = new StreamReader("a.txt"); //默认采用UTF-8编码方式
StreamWriter
继承自TextWriter。
将字符写入到流中。
string text =
@"aa
bb
cc";
StringReader reader = new StringReader(text);
int c = reader.Read();
Console.Write((char)c);
char[] buffer = new char[8];
reader.Read(buffer, 0, buffer.Length);
Console.Write(String.Join("",buffer));
string line = reader.ReadLine();
Console.WriteLine(line);
string rest = reader.ReadToEnd();
Console.Write();
reader.Dispose();
StringReader和StringWriter
也继承自TextReader和TextWriter,但是用来处理字符串。
BinaryWriter和BinaryReader
BinaryWriter用于向流中以二进制方式写入基元类型,比如int, float, char, string等.BinaryReader用于从流中读取基元类型。注意,这2个类并不是继承TextReader和TextWriter。
namespace ConsoleApplication29
{
class Program
{
static void Main(string[] args)
{
Product p = new Product("product.bin")
{
Id = 1,
Name = "GOOD",
Price = 500F
};
p.Save();
Product newP = new Product("product.bin");
newP.Load();
Console.WriteLine(newP);
Console.ReadKey();
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
private string filePath;
public Product(string filePath)
{
this.filePath = filePath;
}
public void Save()
{
FileStream fs = new FileStream(this.filePath, FileMode.Create,FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(this.Id);
writer.Write(this.Name);
writer.Write(this.Price);
writer.Dispose();
}
public void Load()
{
FileStream fs = new FileStream(this.filePath, FileMode.Open,FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
this.Id = reader.ReadInt32();
this.Name = reader.ReadString();
this.Price = reader.ReadDouble();
reader.Dispose();
}
public override string ToString()
{
return String.Format("Id:{0},Name:{1},Price:{2}", this.Id, this.Name, this.Price);
}
}
}
结果:
编码方式:
定义了字节如何转换成人类可读的字符或者文本,可以看作是字节和字符的对应关系表。在读取文件采用的编码方式要和创建文件采用的编码方式保持一致。
帮助类
在System.IO命名空间下。
File
FileStream fs = File.Create("a.txt");
Open(string path, FileMode mode)
OpenRead()
OpenWrite()
ReadAllText()
ReadAllByte()
WriteBllBytes()
WriteAllLines()
Copy(string sourceFileName, string destFileName)
FileInfo
Path
Directory
DirectoryInfo
来源:https://www.cnblogs.com/darrenji/p/3657740.html
猜你喜欢
- 一、使用 System.Xml.Serialization类1、定义元数据引入System.Xml.Serialization命名空间。XM
- 本文实例讲述了Spring实战之清除缓存操作。分享给大家供大家参考,具体如下:一 配置文件<?xml version="1.
- 本文实例为大家分享了Android联系人字母排序的具体代码,供大家参考,具体内容如下实现思路:首先说下布局,整个是一个相对布局,最下面是一个
- 双向顺序队列ArrayDeque和双向链式队列LinkedList,JDK已经包含,在此略。ArrayDeque包括顺序栈和顺序队列,Lin
- 本文实例讲述了c#与js随机数生成方法。分享给大家供大家参考。具体如下:1. C#产生随机数方法:Random rd = new Rando
- 需求描述:企业开发过程中,经常需要将一些静态文本数据放到Resources目录下,项目启动时或者程序运行
- 模块调用之后,记录模块的相关日志,看似简单,其实暗藏玄机。1.简述模块日志的实现方式大致有三种:AOP + 自定义注解实现输出指定格式日志
- 本文的目的是用springboot整合mybatis实现一个简单的一对多查询。(查询一个用户有多少件衣服)第一步:数据库中,可以直接在nav
- 本文实例讲述了Java使用桥接模式实现开关和电灯照明功能。分享给大家供大家参考,具体如下:一、模式定义桥接模式,也称桥梁模式,在软件系统中,
- 一. 引入相关jar包首先,我们先在idea里创建出一个Maven项目出来,除了路径要填成自己的希望路径以外,其他都可以一路next和fin
- 概念逃逸分析一种数据分析算法,基于此算法可以有效减少 Java 对象在堆内存中的分配。 Hotspot 虚拟机的编译器能够分析出一个新对象的
- java中Hashmap的get方法map中存储的是键值对,也就是说通过set方法进行参数和值的存储,之后通过get“键”的形式进行值的读取
- 逆转交替合并两个链表,即从一个链表的尾指针指向另一个链表的尾指针,依次逆转交替进行合并。下面就通过实例来详细的介绍该逆转交替合并两个链表的思
- 需要注意的一点是,feign好像是无法传递list集合类型的,但是你可以通过传递对象类型,然后在接收方再次将对象装在集合中达到集合传递的效果
- 下载maven 解压路径: 打开环境变量:右键此电脑-属性-高级系统设置-高级-环境变量添加以下系统变量:测试:win+
- Spring AOP预处理Controller的参数实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用比如有这样
- 具体代码如下所示:public class Parent { public static int a = parentStati
- Spring @Configuration 和 @Component 区别一句话概括就是 @Configuration 中所有带 @Bean
- 概念Java中的集合就是一种容器,可以容纳不同种类的数据,这些容纳是建立在未知的基础上。优点1.可以动态保存任意多个对象,使用比较方便。2.
- 1 依赖配置<parent> <groupId>org.springframework.b