惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
N
News and Events Feed by Topic
Recent Announcements
Recent Announcements
D
Docker
M
MIT News - Artificial intelligence
L
LangChain Blog
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
博客园_首页
MongoDB | Blog
MongoDB | Blog
美团技术团队
S
Schneier on Security
G
GRAHAM CLULEY
月光博客
月光博客
有赞技术团队
有赞技术团队
Vercel News
Vercel News
Scott Helme
Scott Helme
P
Privacy International News Feed
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
Attack and Defense Labs
Attack and Defense Labs
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
量子位
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Visual Studio Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security Affairs
Cloudbric
Cloudbric
爱范儿
爱范儿
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives

博客园 - 爱吃糖豆的猪

Manjaro Samba 设置共享文件夹供Windows访问 【自学Python系列】Python 基础 内部数据结构之列表和元组 【自学Python系列】Python 基础 (字符串,整数,注释) 控制台I/O显示格式化的结果 异步拷贝文件 【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 如何在Directory.GetFiles()方法中设置多个格式呢? C#操作符??和?: ETL 解析 简单通用的访问CVS的方法 一些概念的随笔(转) 3种思维培养有决策力的孩子 是夫妻就一起把它戒了吧! 一个10年SEO工作者的35个SEO经验 可以让你少奋斗10年的工作经验 如何在一年内拥有十年的工作经验(值得你反复读5遍以上) TreeView 递归选择父节点和子节点 C# 中的 ConfigurationManager类引用方法 添加Word,Excel等dll时如何操作。
文件操作
爱吃糖豆的猪 · 2014-12-04 · via 博客园 - 爱吃糖豆的猪

       1.读写文件第一步创建文件流

      语法:

             FileStream 文件对象=new FileStream(string FilePath, FileMode);

             FilePath:      文件路径  

              FileMode:       打开文件的模式,是一个枚举类型

             Create:  用于指定的名称新建一个文件,如果文件存在,则改写旧文件

             CreateNew:  新建文件。如果文件存在会发生异常,提示文件已经存在

             Open:          打开一个文件,使用这个枚举值时,指定的文件不存在,则指定的名称新建一个文件并打开

             OpenOrCream:    OpenOrCream与Open成员相似,只是如果文件不存在,则用指定的名称新建一个文件打开

             Append:       打开文件,并在文件尾追加内容

       2.StreamWriter写入器

             创建文件流后,再创建写入器,StreamWriter 类称为写入器,它用于将数据写入文件流,只要将创建好的文件流传入,就可以创建它的实例

      语法:

             StreamWriter 写入器对象=new StreamWriter(文件流对象);

             StreamWriter.Writer():          用于写入器,

             StreamWriter.WriterLine():    用于写入一行数据,写入数据后换行。

             StreamWriter.Close():           用于关闭写入器

-------------------------示例代码-------------------------

          try

            {

                //创建文件流

                FileStream stream = new FileStream(@"D:\test.txt", FileMode.Create);

                //创建写入器

                StreamWriter writer = new StreamWriter(stream);

                //写入字符串

                string str = Console.ReadLine();

                writer.Write(str);

                //关闭写入器

                writer.Close();

                //关闭文件流

                stream.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }    

-------------------------------------------------------------------------

       2.StreamReader读取器

             创建文件流后,再创建读取器,StreamReader 类称为读取器,它用于将数据写入文件流,只要将创建好的文件流传入,就可以创建它的实例

      语法:

             StreamReader 读取器对象 = new StreamWriter(文件流对象);

             StreamWriter.ReadLine():      用于写入一行数据,写入数据后换行。

             StreamWriter.ReadLine():      从当前位置读到末尾,返回字符串

             StreamWriter.Close():    用于关闭读取器

 try

            {

                //创建文件流

                FileStream stream = new FileStream(@"D:\test.txt", FileMode.Open);

                //创建读取器

                StreamReader reader = new StreamReader(stream,Encoding.GetEncoding("UTF-8"));

                //读取文件所有内容

                string str = reader.ReadToEnd();

                Console.WriteLine(str);

                //关闭读取器

                reader.Close();

                //关闭文件流

                stream.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

----------------------------------------------------------------------

文件(File)和目录(Directory)的操作

       1.File类的常用方法

              (1)Exists(string path):检查指定文件是否存在,返回一个bool值例

              if (File.Exists(@"D:\test.txt"))

                {

                    Console.WriteLine("找到文件!");

                }

              (2)Copy(string SourceFilePath,string DestinationFilePath):将指定的源文件(ourceFilePath)中的内容复制到目标文件(DestinationFilePath)中,如果目标文件不存在,则在指定的路径中新建一个文件

              (3)Move(string SourceFileName,string DestinationFileName): 将指定的文件(ourceFilePath)移动到新的路径(将指定的源文件(ourceFilePath)例: File.Copy(@"D:\test.txt", @"E:\test.txt");

              (4)Delete(string path):删除指定文件,如果指定文件不存在,则不发生异常 File.Delete(@"D:\test.txt");

       2.Directory类的常用的方法

              (1)Exists(string path):                           用于检查指定文件夹在磁盘上是否存在

              (2)Move(string sourceDirName,stirng destDirName):    用于将文件或者目录及其内容移到新的位置

              (3)delete(string,bool):                           删除指定目录,如果bool值为true,则删除子目录的所有目录内容

       3.FileInfo类的属性和方法

              FileInfo 对象名=new FileInfo(@"文件路径");

              (1)Exists属性:           用于检查指定文件是否存在,返回一个bool值 例:FileInfo类的对象.Exists

              (2)Extension属性:      获取表示文件扩展名部分的字符串(后缀名) 例:FileInfo类的对象.Extension

              (3)Name属性:           获取文件名 例:FileInfo类的对象.Name

              (4)FullName属性:      获取目录或文件的完整目录      例:FileInfo类的对象.FullName

              (5)CopyTo(String): 将现有的文件复制到新文件,不允许覆盖现有的文件 例:FileInfo类的对象.CopyTo(String)

              (6)Delete():           永久删除文件                   例: FileInfo类的对象.Delete()

              (7)MoveTo(string): 将指定文件移到新的位置(string)  例: FileInfo类的对象.MoveTo(string)

       4.DirectoryInfo类的属性和方法

              DirectoryInfo 对象名=new  DirectoryInfo(@"文件路径");

              两个重要的方法

                     (1)GetDirectory():该方法返回当前目录的子目录对象数组,Directory也有这个方法,但返回值是当前目录子目录的名称数组

                     (2)GetFiles():该方法返回当前目录下文件列表(FileInfo对象数组),Directory也有这个方法,但返回值是指定目录子目录的名称数组

       5.要注意的是 File类和Directory类都用静态方法操作文件和目录,而FileInfo类和DirectoryInfo类都要实例化

MyFile类的属性

       (1)float类型FileLength属性: 文件长度,以KB为单位

       (2)string类型FileName 属性:文件名

       (3)string类型FilePath 属性:文件路径