读文件示例

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读写文件的更多相关文章

  1. FileStream读写文件流

    用FileStream 读取文件流并显示给文件内容 string p = @"C:\Users\Administrator\Desktop\1.txt"; FileStream f ...

  2. [转载]FileStream读写文件

    FileStream读写文件 FileStream类:操作字节的,可以操作任何的文件 StreamReader类和StreamWriter类:操作字符的,只能操作文本文件. 1.FileStream类 ...

  3. FileStream读写文件【StreamWriter 和 StreamReader】

    FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能.这是因为FileStream类 ...

  4. C#读写文件总结

    1.使用FileStream读写文件   文件头:   using System; using System.Collections.Generic; using System.Text; using ...

  5. C# 之 读写文件

    1.使用 FileStream 读写文件 添加命名空间引用: using System; using System.Collections.Generic; using System.Text; us ...

  6. c# 高效读写文件

    一.同步读写文件(在并发情况下不会发生文件被占用异常) static void Main(string[] args) { Parallel.For(0, 10000, e => { strin ...

  7. C#常用IO流与读写文件

    .文件系统 ()文件系统类的介绍 文件操作类大都在System.IO命名空间里.FileSystemInfo类是任何文件系统类的基类:FileInfo与File表示文件系统中的文件:Directory ...

  8. c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件

    c# 读写文件时文件正由另一进程使用,因此该进程无法访问该文件,在IO处理上遇到了无法操作的问题. 文件"D:\log.txt"正由另一进程使用,因此该进程无法访问该文件. log ...

  9. 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

    MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

随机推荐

  1. JXLS导出Excel(模板导出)

    1.导包 在pom.xml中加入依赖如下: <dependency> <groupId>org.jxls</groupId> <artifactId>j ...

  2. SpringBoot自动配置的实现原理

    之前一直在用SpringBoot框架,一直感觉SpringBoot框架自动配置的功能很强大,但是并没有明白它是怎么实现自动配置的,现在有空研究了一下,大概明白了SpringBoot框架是怎么实现自动配 ...

  3. 2-6-搭建FTP服务器实现文件共享

    ---------------------------------------------- 大纲: FTP服务:(文件传输协议---> File Transfer Protocol) 常见FT ...

  4. [转载]宿主机为linux、windows分别实现VMware三种方式上网,Host-only win10+rhel7.2实现

    研究一下虚拟机三种上网方式 转自:http://linuxme.blog.51cto.com/1850814/389691 一.VMware三种方式工作原理1 Host-only连接方式 让虚机具有与 ...

  5. Qt5.3中qml ApplicationWindow设置窗口无边框问题

    这个版本的qt在这里有点bug.. 设置ApplicationWindow的flags属性为Qt.FramelessWindowHint的确可以使程序无边框,但是同时程序在任务栏的图标也没了. 看文档 ...

  6. qt5.2.1在linux下去除最大化和最小化按钮

    #include <QtGui/QGuiApplication> #include <QDebug> #include <QScreen> #include &qu ...

  7. IOS-源代码管理工具(SVN)

    一.使用环境 要想利用SVN管理源代码,必须得有2套环境 服务器 用于存储客户端上传的源代码 可以在Windows上安装Visual SVN Server 大部分情况下,公司的开发人员不必亲自搭建SV ...

  8. LNMP的搭建与原理

    常见的web框架结构:例如:lnmp和:ampL=LINUX N=NGINX A=APACHE P=php T=Tomcat lnmp的原理 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Ng ...

  9. hdu4115

    题解: 2-sat 对于bob出的每一张牌,alice显然只有两种选择 然后对于每一个限制,连边 判断是否可行 代码: #include<cstdio> #include<cmath ...

  10. UI-基本控件的简单使用

    1.IBAction:   //====================== 1> 能保证方法可以连线 2> 相当于void 2.IBOutlet: 1> 能保证属性可以连线 3.常 ...