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 ...
随机推荐
- PHP中IP地址与整型数字互相转换详解
这篇文章主要介绍了PHP中IP地址与整型数字互相转换详解,本文介绍了使用PHP函数ip2long与long2ip的使用,以及它们的BUG介绍,最后给出自己写的两个算法,需要的朋友可以参考下 IP转换成 ...
- HBase 高性能加入数据 - 按批多“粮仓”式解决办法
摘要:如何从HBase中的海量数据中,以很快的速度的获取大批量数据,这一议题已经在<HBase 高性能获取数据>(http://www.cnblogs.com/wgp13x/p/42451 ...
- Lighttpd
一.简介 Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全.快速.兼容性好并且灵活的web server环境.具有非常低的内存开销,cpu占用率 ...
- 查看Linux服务器内存使用情况
一个服务器,最重要的资源之一就是内存,内存够不够用,是直接关系到系统性能的关键所在. 本文介绍如何查看Linux服务器内存使用情况, 1.free命令 free -m [root@localhost ...
- C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度
在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...
- css shorthand属性简写
一.什么是shorthand 属性简写(shorthand)就是一次性声明一组相关的属性.好处呢当然是众所周知的,让css从臃肿无序升级为简洁有效具有高可读性. 大多数的人都使用属性简写,我也用,但是 ...
- [转]【HTTP】Fiddler(二) - 使用Fiddler做抓包分析
本文转自:http://blog.csdn.net/ohmygirl/article/details/17849983 上文( http://blog.csdn.net/ohmygirl/articl ...
- codeforces 721B B. Passwords(贪心)
题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Topcoder SRM 618 Div2 --900
题意:给定两个NxN的棋盘,每个棋盘都有一个‘车’的摆放状态,问进行若干次交换,能否使棋盘1变为棋盘2. 交换规则:每次选两个‘车’,坐标分别为(r1,c1),(r2,c2),如果r1<r2并且 ...
- FreeMarker 一二事 - 静态模板的使用与生成
如今前后端分离,动静分离 使用freemarker实现动静分离,nginx处理静态资源文件,提高效率 加载jar包 <!-- freemarker --> <dependency&g ...