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 ...
随机推荐
- 引发事件代码封装成OnEventName
引发事件的代码,通常可以封装成“On+事件名称”的方法(On:表示当“什么什么”的时候),如下所示: 1:引发事件代码: if (PropertyChanged != null)//为了实现将数据源的 ...
- 《OpenCL编程指南》之 与Direct3D互操作
介绍OpenCL与D3D 10之间的互操作. 1.初始化OpenCL上下文实现Direct3D互操作 OpenCL共享由pragma cl_khr_d3d10_sharing启用: #pragma O ...
- logback配置日志输出
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> & ...
- GlusterFS原创资源
学习博客: GlusterFS原创资源
- PyCharm在win10的64位系统安装实例
搭建环境 1.win10_X64,其他Win版本也可以. 2.PyCharm版本:Professional-2016.2.3. 搭建准备 1.到PyCharm官网下载PyCharm安装包. 2.选择W ...
- 使用Eclipse EE(汉化版) 创建一个JavaWeb工程
第一步:打开eclipse ee,单击“文件”-->单击“新建”-->单击“动态Web项目”. 若没找到“动态Web项目”,单击“其他” -->在弹出的窗口中打开“Web”下拉菜单 ...
- Bing的Translation API 接入
参考: https://msdn.microsoft.com/zh-cn/library/mt146806.aspx 首先你需要一个Microsoft的帐号,如果没有在这里注册一下 https://s ...
- 关于Qt中使用线程的时候函数具体在哪个线程中运行的问题
在子线程中,run函数中以及其中调用的都在单独的子线程里面运行,但是其他的类似构造函数之流都是在主线程里面运行的,不要搞混了
- QT出现 Cannot create children for a parent that is in a different thread 的解决方法:
timer = new Timer(this);改成 timer = new Timer();就可以了. 因为你timer是属于主线程的,尽量不要在非主线程里创建新的对象并指定其对象为主线程内的对象, ...
- popViewControllerAnimated 后的刷新问题
popViewControllerAnimated后,进入的viewcontroller不能即时刷新. 这时它不执行viewDidLoad,但执行viewWillAppear:(BOOL),所以只要把 ...