C#:IO
1. File和Directory类
这两个类属于静态类,经常用到的比如File.Exists(string path), Directory.Exists(string path), Directory.GetCurrentDirectory(), Directory.SetCurrentDirectory(string path);
另外还有FileInfo和DirectoryInfo类,属于实例类,用法跟File和Directory差不多,就是要先实例化出来
当单一调用的时候优先用File和Directory类,如果操作很多,则用FileInfo和DirectoryInfo类会更好
2. FileStream类
用于指向文件的流操作,读写文档没StreamReader和StreamWriter方便
当然FileStream类最牛逼的地方在于Seek(long offset, SeekOrigin)这个方法
FileStream(string fileName, FileMode);
FileStream(string fileName, FileMode, FileAccess);
public enum FileMode
{
Append,
Create,
CreateNew,
Open,
OpenOrCreate,
Truncate
}
public enum FileMode
public enum FileAccess
{
Read,
ReadWrite,
Write
}
public enum FileAccess
3. FileWriter
虽然FileWriter能够直接对文件写入,但是没有更多选项,所以一般要先创建一个FileStream,再创建FileWriter
4.FileReader
与FileWriter一样,但是FileStream的FileMode要改改
Read的时候可能会有文件不存在的BUG,所以要么加上try..catch,要么先判断exists
5. Path类
静态类,主要有GetDirectoryName和GetFileName两个方法
下面这段代码集成了上面的内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using System.IO;
namespace test4
{
class Program
{
const string FILE = @"C:\Users\Administrator\Desktop\test.txt";
const string DIRECTORY = @"C:\Users\Administrator\Desktop\";
static void Main(string[] args)
{
if (File.Exists(FILE))
{
Console.WriteLine("test.txt exist on desktop");
}
if (Directory.Exists(DIRECTORY))
{
Console.WriteLine("Desktop directory exists");
}
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine(Path.GetDirectoryName(FILE));
Console.WriteLine(Path.GetFileName(FILE));
//FileStream fst = File.OpenRead(FILE);
//StreamWriter sw = new StreamWriter(FILE, true);
FileStream fst = new FileStream(FILE, FileMode.Append | FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fst);
sw.WriteLine("xueyiyi");
sw.Close();
fst.Close();
//try
//{
// fst = new FileStream(FILE, FileMode.Open);
// StreamReader sr = new StreamReader(fst);
// string str;
// while ((str = sr.ReadLine()) != null)
// {
// Console.WriteLine(str);
// }
// sr.Close();
// fst.Close();
//}
//catch (IOException e)
//{
// Console.WriteLine("An IO exception has been thrown!");
// Console.WriteLine(e.ToString());
// return;
//}
if (File.Exists(FILE))
{
fst = new FileStream(FILE, FileMode.Open);
StreamReader sr = new StreamReader(fst);
string str;
while ((str = sr.ReadLine()) != null)
{
Console.WriteLine(str);
}
sr.Close();
fst.Close();
}
else
{
Console.WriteLine(FILE + " does not exist");
}
}
}
}
用StreamWriter类可以简单实现文件写入
StreamWriter s = new StreamWriter(address + "/Menu.ini", true);s.WriteLine(openFileDialog1.FileName);s.Flush();s.Close();StreamReader sr = new StreamReader(address + "/Menu.ini");while (sr.Peek()>=0){ string str = sr.ReadLine();}sr.Close();C#:IO的更多相关文章
- 谈谈我的微软特约稿:《SQL Server 2014 新特性:IO资源调控》
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 撰写经历(Experience) 特约稿正文(Content-body) 第一部分:生活中资源 ...
- 泛函编程(38)-泛函Stream IO:IO Process in action
在前面的几节讨论里我们终于得出了一个概括又通用的IO Process类型Process[F[_],O].这个类型同时可以代表数据源(Source)和数据终端(Sink).在这节讨论里我们将针对Proc ...
- 泛函编程(32)-泛函IO:IO Monad
由于泛函编程非常重视函数组合(function composition),任何带有副作用(side effect)的函数都无法实现函数组合,所以必须把包含外界影响(effectful)副作用不纯代码( ...
- SQL Server 2014 新特性:IO资源调控
谈谈我的微软特约稿:<SQL Server 2014 新特性:IO资源调控> 2014-07-01 10:19 by 听风吹雨, 570 阅读, 16 评论, 收藏, 收藏 一.本文所涉及 ...
- Java NIO:IO与NIO的区别
一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...
- 字节输入流:io包中的InputStream为所有字节输入流的父类。
字节输入流:io包中的InputStream为所有字节输入流的父类. Int read();读入一个字节(每次一个): 可先使用new byte[]=数组,调用read(byte[] b) read ...
- 14:IO之字符字节流
字节流: InputStream OutputStream 字节流: FileInputStream FileOutputStream BufferedInputStream Buffer ...
- Java基础:IO流之字节流和字符流
1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...
- Java网络编程和NIO详解3:IO模型与Java网络编程模型
Java网络编程和NIO详解3:IO模型与Java网络编程模型 基本概念说明 用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32 ...
- Java NIO:IO与NIO的区别 -阿里面试题
一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...
随机推荐
- 【转】UnityVS(Visual Studio Tools For Unity)的安装与使用
Unity 的开发者们,尤其是微软系的Unity开发者们,用Mono是不是烦死了?你是不是跟我一样,用vs来写代码,用Mono来跟踪调试?好麻烦啊好麻烦. 也许你会说,傻逼你不会用UnityVS插件么 ...
- pylint window下安装与使用
简介 Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8)和有潜在问题的代码. Pylint ...
- Mschat控件示例升级错误处理方法
将具有 3.5 版图表控件的 ASP.NET 3.5 网站升级到 ASP.NET 4 需要更改 web.config 和注册指令 将具有 3.5 版图表控件的 ASP.NET 3.5 网站升级到 AS ...
- Python基本数据类型之list列表
列表是python中用的非常频繁的数据结构,它是有序序列.之前学的字符串就是一种有序序列.不过列表是可变的. 创建列表 li = list( ) #构 ...
- 转:php 获取时间今天明天昨天时间戳
<?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...
- windows2008一键安装环境的配置说明
windows 2008 一键安装包下载地址为 http://gongdan.oss-cn-hangzhou.aliyuncs.com/market/cmISV/34320/product/cmgj0 ...
- Arbitrage---poj2240(floyd)
题目链接:http://poj.org/problem?id=2240 题意:有n个国家的,有m个关系,每个关系的格式是:A B C表示1单位的A国货币可以换B单位C国货币:求是否存在一种方法使得货币 ...
- undefined reference to `switch_dev_unregister'
编译内核时,使用默认的配置进行编译.出现错误:undefined reference to switch_dev_unregister',undefined reference toswitch_se ...
- linux i2c tools
最近要操作eeprom,所以了解一下i2c-tool的使用方法,记录于此. 参考链接: http://www.myir-tech.com/bbs/thread-7567-1-1.html http:/ ...
- 转帖:解决jquery.js在myeclipse中报错的问题
转载地址:http://tieba.baidu.com/p/1993753087 从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在my ...