Stream 流操作

- 可以读取流。读取是从流到数据结构(如字节数组)的数据传输。
- 可以写入流。写入是从数据结构到流的数据传输。
- 流可以支持查找。查找是对流内的当前位置进行查询和修改。查找功能取决于流具有的后备存储区类型。例如,网络流没有当前位置的统一概念,因此一般不支持查找。



using System; using System.IO; using System.Text; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (File.Exists(path)) { File.Delete(path); } //Create the file. using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); ;i < ;i++) { AddText(fs, Convert.ToChar(i).ToString()); } } //Open the stream and read it back. using (FileStream fs = File.OpenRead(path)) { ]; UTF8Encoding temp = new UTF8Encoding(true); ,b.Length) > ) { Console.WriteLine(temp.GetString(b)); } } } private static void AddText(FileStream fs, string value) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, , info.Length); } }

using System; using System.IO; class Test { public static void Main() { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } }



using System; using System.IO; using System.Text; namespace ConsoleApplication { class Program { static void Main(string[] args) { ReadCharacters(); } static async void ReadCharacters() { StringBuilder stringToRead = new StringBuilder(); stringToRead.AppendLine("Characters in 1st line to read"); stringToRead.AppendLine("and 2nd line"); stringToRead.AppendLine("and the end"); using (StringReader reader = new StringReader(stringToRead.ToString())) { string readText = await reader.ReadToEndAsync(); Console.WriteLine(readText); } } } } // The example displays the following output://// Characters in 1st line to read// and 2nd line// and the end//
using System; using System.IO; public class TextToFile { private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args) { if (File.Exists(FILE_NAME)) { Console.WriteLine("{0} already exists.", FILE_NAME); return; } using (StreamWriter sw = File.CreateText(FILE_NAME)) { sw.WriteLine ("This is my file."); sw.WriteLine ("I can write ints {0} or floats {1}, and so on.", , 4.2); sw.Close(); } } }

名称 | 说明 |
BinaryReader (Stream) | 基于所提供的流,用 UTF8Encoding 初始化 BinaryReader 类的新实例。 由 .NET Compact Framework 支持。 |
BinaryReader (Stream, Encoding) | 基于所提供的流和特定的字符编码,初始化 BinaryReader 类的新实例。 由 .NET Compact Framework 支持。 |
public AppSettings() { // Create default application settings. aspectRatio = 1.3333F; lookupDir = @"C:\AppDirectory"; autoSaveTime = ; showStatusBar = false; if(File.Exists(fileName)) { BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)); try { // If the file is not empty, // read the application settings. ) { aspectRatio = binReader.ReadSingle(); lookupDir = binReader.ReadString(); autoSaveTime = binReader.ReadInt32(); showStatusBar = binReader.ReadBoolean(); } } // If the end of the stream is reached before reading // the four data values, ignore the error and use the // default settings for the remaining values. catch(EndOfStreamException e) { Console.WriteLine("{0} caught and ignored. " + "Using default values.", e.GetType().Name); } finally { binReader.Close(); } } }





如果使用FileAccess.Write,则无论FileShare是什么值其他进程都无法再次写入,不过配合合适的FileShare其他进程还是可以Read)


using System; using System.IO; class MyStream { private const string FILE_NAME = "Test.data"; public static void Main(String[] args) { // Create the new, empty data file. if (File.Exists(FILE_NAME)) { Console.WriteLine("{0} already exists!", FILE_NAME); return; } FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew); // Create the writer for data. BinaryWriter w = new BinaryWriter(fs); // Write data to Test.data. ; i < ; i++) { w.Write( (int) i); } w.Close(); fs.Close(); // Create the reader for data. fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // Read data from Test.data. ; i < ; i++) { Console.WriteLine(r.ReadInt32()); } r.Close(); fs.Close(); } }
Stream 流操作的更多相关文章
- 还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下
Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda ...
- 超强的Lambda Stream流操作
原文:https://www.cnblogs.com/niumoo/p/11880172.html 在使用 Stream 流操作之前你应该先了解 Lambda 相关知识,如果还不了解,可以参考之前文章 ...
- 全面吃透JAVA Stream流操作,让代码更加的优雅
全面吃透JAVA Stream流操作,让代码更加的优雅 在JAVA中,涉及到对数组.Collection等集合类中的元素进行操作的时候,通常会通过循环的方式进行逐个处理,或者使用Stream的方式进行 ...
- Node学习笔记(一):stream流操作
NodeJs中谈及较多的可能就是Stream模块了,先写一个简单的ajax回调 $.post("index.php",{data:'aaa',order:'ccc'},functi ...
- stream流操作List工具类
工作中操作List对于程序猿来说是"基本操作",为了更加便利,对JDK8的新特性stream流进行二次封装.话不多说,直接上代码 package com.mydemo; impor ...
- lamda表达式与Stream 流操作,reduce,flatMap,groupingBy等
/** * 符合lambda表达式的接口也叫函数式接口: * 除了默认方法和Object类的方法外,只有一个抽象方法的接口才能符合lambda表达式的要求 * 可以使用@FunctionalInter ...
- 深度掌握 Java Stream 流操作,让你的代码高出一个逼格!
概念 Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选.排序.聚合等. Stream 的操作符大体上分为两种:中间操作符和终止操作符 中 ...
- .net System.IO.Stream 流操作类(FileStream等)
Stream 是所有流的抽象基类.流是字节序列的抽象概念. 流涉及到的3个基本操作: 读取流,读取是指从流到数据结构(如字节数组)的数据传输. 写入流,写入是指从数据结构到流的数据传输. 流查找,查找 ...
- java8 stream流操作的flatMap(流的扁平化)
https://mp.weixin.qq.com/s/7Fqb6tAucrl8UmyiY78AXg https://blog.csdn.net/Mark_Chao/article/details/80 ...
随机推荐
- 一个不错的shell 脚本教程 入门级
一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂 建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行s ...
- CSS中的 backgroundPosition 属性
body { background-image:url('bgimage.gif'); background-repeat:no-repeat; background-attachment:fixed ...
- 17110 Divisible(basic)
17110 Divisible 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Given n + m integers, I1,I2,. ...
- MAPR 开发环境搭建过程记录
我下载了MAPR 官方提供的virtualbox 和 vmware版本的sandbox进行试用. 开始试用了一会vmware版的,因为不太熟悉vmware的操作,而且vmplayer经常没有反应,后来 ...
- [转]Javascript中的自执行函数表达式
[转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...
- 分享用于学习C++图像处理的代码示例
为了便于学习图像处理并研究图像算法, 俺写了一个适合初学者学习的小小框架. 麻雀虽小五脏俱全. 采用的加解码库:stb_image 官方:http://nothings.org/ stb_image. ...
- 合工大 OJ 1322 窗口
窗口 Description 在某图形操作系统中,有N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里, ...
- 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS
原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...
- 2014 Super Training #2 C Robotruck --单调队列优化DP
原题: UVA 1169 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...