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 ...
随机推荐
- 烂泥:虚拟化KVM安装与配置
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近打算把公司的服务器全部做成虚拟化,一是跟有效的利用了服务器,二也是对自己是一个学习的机会. KVM的安装与配置步骤如下: 1. 查看是否支持虚拟化 ...
- Provides PHP completions for Sublime Text
来源:https://packagecontrol.io/packages/PHP%20Completions%20Kit php-completions php-completions plugin ...
- ProjectManager Alpha8 - 项目管理器,管理起开发中的项目很方便
话不多说= = 放几张图了: 文件下载: 32位下载:Package_ProjectManager-1.13.12.exe 64位下载:Package_ProjectManager_x64-1.13. ...
- 虚拟机centos6.5 --设置静态ip
编辑网卡文件,vi /etc/sysconfig/network-scripts/ifcfg-eth0修改以下内容: ONBOOT=no #改为yes, BOOTPROTO=dbcp #改为stati ...
- Dos
一.简介 https://zh.wikipedia.org/wiki/DOS 二.系统下载 http://www.cn-dos.net/newdos/doswarea.htm 三.实用命令 1)查看系 ...
- xamarin.android 给View控件 添加数字提醒效果-BadgeView
本文代码从java项目移植到.net项目 java开源项目:https://github.com/jgilfelt/android-viewbadger using System; using S ...
- linux timezone
首先需要了解下“UTC时间”与“本地时间”UTC时间:世界协调时间(又称世界标准时间.世界统一时间),在一般精度要求下,它与GMT(Greenwich Mean Time,格林威治标准时间)是一样的, ...
- c# 参数传递
c#类型有值类型与引用类型. 无论哪种类型的变量,作为方法的参数进行传递时,默认是以"值传递"方式来传递的. 传递给方法的形参,在执行时都会新创建一个局部变量,然后接受实参的值, ...
- 【jQuery Demo】jQuery打造动态下滑菜单
作者:漫凯维奇 来源:[教程]jQuery打造动态下滑菜单 Tip:这只是一个转载,源代码可以在上面的来源博文中下载 此教程将分步讲解如何使用JQuery和CSS打造一个炫酷动感菜单.效果如 ...
- hdu 5802 Windows 10 贪贪贪
传送门:hdu 5802 Windows 10 题意:把p变成q:升的时候每次只能升1,降的时候如果前一次是升或者停,那么下一次降从1开始,否则为前一次的两倍 官方题解: 您可能是正版Windows ...