IO Operation
来源:互联网 发布:知乎淘宝码头仓库总店 编辑:程序博客网 时间:2024/04/26 17:33
io操作中常使用的类包括有FileStream,File,Directory,Path,FileInfo, DirectoryInfo,FileStreamInnfo,StreamReader,StreamWriter,FileSystemWatcher,上面的类存在于System.IO命名空间中,另外在System.IO.Compression命名空间中还存在下面的两个类,DeflateStream和GZipStream,这两个类分别允许使用Deflate和gzip模式来读写压缩文件。
1.File and Directory class
方法说明Copy将文件复制到目的地址Create用于在规定的路径创建一个文件Delete删除文件Open打开文件,返回的是FileStreamMove实现的是文件的转移需要注意的是File市System.IO中的静态成员,所以在使用是不能直接new,而是直接使用本身
File.Open (@”PATH”)
方法说明CreateDirectory创建指定的路径Delete删除。。GetDirectories放回当前目录名GetFiles得到当前目录下的文件名的string数组GetFilesSystemEntries除上面的文件名之外还包括的有目录名Move将指定的目录移动到其他的位置Directory同样是static class,使用同上
2.FileInfo class
属性说明Directory返回的是DirectoryInfoDirectoryName文件目录名IsReadOnly文件的属性read onlyLength文件的长度FileInfo fileInfo = new FileInfo(@"c:/test.txt");创建FileInfo对象,然后使用上面的属性
3.DirectoryInfo class
属性说明Parent返回DirectoryInfo对象Root当前目录的根目录(例如c:/)此处需要注意的是相对路径和绝对路径,绝对路径没什么可讲的,但是相对路径的话,在vs2008中启动debug一个程序的话,当前路径指的是ProgramName/bin/debug,如果需要可以使用Directory.GetCurrentDirectory()找出当前的工作目录,或者使用Directory.SetCurrentDirectory来设置当前目录。例如Console.WriteLine ( Directory.GetCurrentDirectory() );
4.FileStream class
成员说明Append追加Create如果文件存在,首先删除CreateNew如果文件存在,exceptionOpen打开现有文件OpenOrCreate打开或者是创建Truncate文件截断为0Seek文件指针文职移动Read读取数据
public override int Read(byte[] array, int offset, int count);该函数读取的是byte[]类型的数据,所以需要decodeWrite向文件中写入数据,基本的思路char to byte,然后调用Write Method来实现将string类型写入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IOTest
{
class Program
{
static void Main(string[] args)
{
byte[] byData = new byte[100];
char[] chData = new char[100];
// make the FileStream
FileStream stream =
new FileStream(Directory.GetCurrentDirectory() + @"/a.txt", FileMode.Open);
// read the date from the file
stream.Read(byData, 0, 100);
// decode the byte array
Decoder decoder = Encoding.UTF8.GetDecoder();
decoder.GetChars(byData, 0, byData.Length, chData, 0);
// display the message
Console.WriteLine(chData);
Console.ReadKey();
}
}
}
// write a message to a text file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IOTest
{
class Program
{
static void Main(string[] args)
{
char[] msg = ("hello world").ToCharArray();
byte[] byMsg = new byte[msg.Length];
Encoder encoder = Encoding.UTF8.GetEncoder();
int charsUsed, bytesUsed;
bool completed;
encoder.GetBytes(msg, 0, msg.Length, byMsg, 0, true);
FileStream stream = new FileStream(@"./a.txt", FileMode.Create);
stream.Write(byMsg, 0, byMsg.Length);
Console.ReadKey();
}
}
}
5.StreamWrite class writes string to file without converting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IOTest
{
class Program
{
static void Main(string[] args)
{
string msg = "hello c#";
FileStream stream = new FileStream(@"./a.txt", FileMode.Create);
StreamWriter writer = new StreamWriter(stream);
writer.Write(msg);
writer.Flush();
Console.ReadKey();
}
}
}
StreamWriter负责将数据传递给FileStram class。大大简化了IO操作
6.StreamReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IOTest
{
class Program
{
static void Main(string[] args)
{
FileStream stream = new FileStream(@"./a.txt", FileMode.Open);
StreamReader reader = new StreamReader(stream);
string msg = reader.ReadLine();
Console.WriteLine(msg);
Console.ReadKey();
}
}
}
未完,待续…
- IO Operation
- C++ file IO operation
- IO Operation – continue
- FSD Direct IO File Operation Library Source
- Linux IO Scheduler -- NOOP(no operation)
- operation
- java.io.IOException: CreateProcess error=740, The requested operation requires elevatio
- Operation PRI
- Operation Password
- Database Operation
- Operation System
- Office Operation
- Division Operation
- file operation
- xml operation
- File Operation
- Clipboard operation
- string operation
- c#操作word 在指定书签插入文字或者图片
- C#操作Word书签实例浅析
- 《伟大的博弈》读后感
- apache php5 配置
- c++代码优化
- IO Operation
- 关于unicode
- Adobe reader 9.0关联文件
- 我看微软.NET各子技术领域之应用前景
- 成功注册了csdn
- C++ Primer 学习笔记(4): 参数传递
- 凌志上证指数交易系统网
- samba配置汇总
- 软件项目经理所必需具备的素质