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 ...
随机推荐
- MongoDB基于GridFS管理文件
前言 GridFS是一种将大型文件存储在MongoDB的文件规范: 数据库支持以BSON格式保存二进制对象. 但是MongoDB中BSON对象最大不能超过4MB. GridFS 规范提供了一种透明的机 ...
- netty-socketio整合springboot消息推送
netty-socketio整合springboot消息推送 1.netty-socketio消息推送 1)在项目中常常涉及到消息推送的情况,消息推送要求的实时性,使用传统的方式已经不能满足需求了: ...
- 35-THREE.JS 多面体
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- 011PHP基础知识——运算符(四)
<?php /** * 连接运算符: . 连接2个参数生成新的字符串: */ /*$str="中国"; $bbs="bbs.blog.com"; $new ...
- [WinForm]Dundas Chart控件学习(附源码)
1.Dundas公司简介 加拿大的一家公司,专业做图表展现的,很牛,据说现在被Microsoft收购了.官网地址:http://www.dundas.com/ 2.Chart基本要素 3.最简单的柱状 ...
- SqlServer和MySQL中存储过程out返回值处理C#代码
1.SqlServer中out处理 C#代码 #region"SqlServer中存储过程处理out返回值" //public void getdata() //{ // stri ...
- ZOJ 2283 Challenge of Wisdom 数论,Dilworth Theorem,求最长反链 难度:2
Challenge of Wisdom Time Limit: 2 Seconds Memory Limit: 32768 KB Background "Then, I want ...
- 四、dbms_alert(用于生成并传递数据库预警信息)
1.概述 作用:用于生成并传递数据库预警信息.使用包DBMS_ALERT,则必须以SYS登陆,为该用户授予执行权限.sql>conn sys/oracle as sysdbasql>gra ...
- nginx的编译,和简单的配置问题
反向代理常见的lvs.haproxy. 缓存服务常见的.squid.vanish.常见的前端缓存.Apache是多进程的web服务器,Nginx是多线程的web服务器. Nginx的特点,对静态能力强 ...
- EasyUI Combobox 组合框
默认选择第一项,加请选择,取值问题 <input class="easyui-combobox" id="ddlWHS" name="ddlWH ...