FileStream读写文件
读文件示例
try
{
// 打开文件
FileStream fs = new FileStream("D:\\not.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
// 读取文件
string strLine = sr.ReadLine();
while (strLine != null)
{
Console.WriteLine(strLine);
strLine = sr.ReadLine();
}
// 关闭文件
sr.Close();
fs.Close();
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine("File Not Found");
}
catch (System.Exception e)
{
Console.WriteLine("Exception");
}
写文件示例
try
{
FileStream fs = new FileStream("D:\\test.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("aaa");
sw.WriteLine("bbb");
sw.Close();
fs.Close();
}
catch (System.UnauthorizedAccessException e)
{
Console.WriteLine("No Access To Write");
}
catch (System.Exception e)
{
Console.WriteLine(e.ToString());
}
写入文件时经常需要去除文件的只读属性
System.IO.File.SetAttributes(strFileFullPath, System.IO.FileAttributes.Normal);
例如:
if (File.Exists("D:\\test.txt"))
{
System.IO.File.SetAttributes("D:\\test.txt", System.IO.FileAttributes.Normal);
}
文件指针
FileStream::Seek(long offset, SeekOrigin origin);
FileMode
CreateNew
// 1.[可读][可写]
// 2.文件不存在,则创建新文件
// 文件已经存在则抛异常
// 3.可移动文件指针
Create
// 1.[可读][可写]
// 2.文件不存在,则创建新文件
// 文件已经存在,则覆盖掉
// 3.可移动文件指针
Open
// 1.[可读][可写]
// 2.文件存在则打开
// 文件不存在则抛异常
// 3.可移动文件指针
OpenOrCreate
// 1.[可读][可写]
// 2.文件存在则打开
// 文件不存在则创建
// 3.可移动文件指针
Truncate
// 1.[可写]
// 2.文件存在则打开并清空文件内存
// 文件不存在则抛异常
// 3.可移动文件指针
FileMode.Append
// 1.[可写]
// 2.文件存在则打开,并将文件指针移至文件末尾
// 文件不存在则创建
// 3.不可移动文件指针,否则会抛异常
FileStream读写文件的更多相关文章
- FileStream读写文件流
用FileStream 读取文件流并显示给文件内容 string p = @"C:\Users\Administrator\Desktop\1.txt"; FileStream f ...
- [转载]FileStream读写文件
FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...
- FileStream读写文件【StreamWriter 和 StreamReader】
FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能.这是因为FileStream类 ...
- C#读写文件总结
1.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using ...
- C# 之 读写文件
1.使用 FileStream 读写文件 添加命名空间引用: using System; using System.Collections.Generic; using System.Text; us ...
- c# 高效读写文件
一.同步读写文件(在并发情况下不会发生文件被占用异常) static void Main(string[] args) { Parallel.For(0, 10000, e => { strin ...
- C#常用IO流与读写文件
.文件系统 ()文件系统类的介绍 文件操作类大都在System.IO命名空间里.FileSystemInfo类是任何文件系统类的基类:FileInfo与File表示文件系统中的文件:Directory ...
- c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件
c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件,在IO处理上遇到了无法操作的问题. 文件"D:\log.txt"正由另一进程使用,因此该进程无法访问该文件. log ...
- 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。
MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
随机推荐
- PowerDesigner用法和技巧
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- h5 沉浸式状态栏
1. manifest.json的plus节点里面配置 "plus": { "statusbar": {"immersed": true}, ...
- [洛谷U62358]求导函数
U62358 求导函数 题面 给出一个n次函数\(f(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+...+a_{1}x+a_0\)的各项系数\(a_n,a_{n-1}...a_1,a_0 ...
- ana3+opencv+TensorFlow+NVIDIAGPU 安装
http://blog.csdn.net/qq_30611601/article/details/79067982 这个博客写的挺完整的 当你发现你的anna下载的贼鸡儿的慢,你就需要使用清华的镜像网 ...
- 【Python】改变对象的字符串显示
问题 改变对象实例的打印或显示输出,让它们更具可读性. 解决方案 要改变一个实例的字符串表示,可重新定义它的 __str__() 和 __repr__() 方法.例如: class Pair: def ...
- ckeditor5富文本编辑器在vue中的使用
安装依赖: npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic 要创建编辑器实例,必须首先将编辑器 ...
- Fast Walsh-Hadamard Transform
这玩意最近经常出现额…… FFT解决的问题是\[C_{k}=\sum_{i+j=k}A_i \cdot B_j\] 其中\(A\).\(B\).\(C\)是三个列向量. 而FWHT是将\(\sum\) ...
- 使用游标、存储过程、pivot 三种方法导入数据
--使用游标循环 if (exists (select * from sys.objects where name = 'Base_RecordTend_Test')) drop proc Base_ ...
- SQL2008R2 收缩数据库问题 - 日志文件不变小
数据库的日志文件(*.ldf)越来越大,怎么办? 收缩吧.收缩日志文件的操作真不简单哟,还跟数据库的恢复模式有关啊. 一.“简单恢复模式”时的日志收缩 1. 截断日志 当数据库的恢复模式为“简单”的时 ...
- SpringInAction--Bean的作用域
Spring定义了多种作用域,我们在使用的时候可以根据使用的需求来选择对应的作用域,这些作用域,包括(第二个括号中为更安全的注解方法,具体更多参数可查看接口代码) 单例(Singleton)(Conf ...