流的分类

存取位置分类:
FileStream
MemoryStream
BufferedStream

读写类:
BinaryReader/BinaryWriter
TextReader/TextWriter

StreamReader/StreamWriter
StringReader/StringWriter

相互关系:

FileStream fin = new FileStream(@"Path", FileMode.Open, FileAccess.Read );
StreamReader brin = new StreamReader(fin, System.Text.Encoding.Default );

FileStream

  • Seek
  • Read

StreamReader

实例:new StreamReader(path, FileMode)

  • ReadLine
  • ReadToEnd
  • Read
  • Peek
  • Close

StreamWriter

实例:new StreamWriter(path, FileMode, FileEncode)

  • WriteLine
  • Write
  • Flush
  • Close
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.IO;

public class Logger
{
static void Main()
{
LogToFile( "msg", true, true );
}

public static string LogFile = @"c:\aaa.log";

public static void LogToFile( string str, bool bWithTime, bool bAppendLineFeed)
{
if( str == null ) return;

try
{
string fname = LogFile;

if( fname == "" || fname == null ) return;

StreamWriter writer = new StreamWriter( fname, true, System.Text.Encoding.Default );
if( bWithTime )writer.WriteLine( "\r\n\r\n--------- " + DateTime.Now.ToString() );

if( bAppendLineFeed ) writer.WriteLine( str );
else writer.Write( str );

writer.Close();
}
catch(Exception e )
{
Console.Write( e );
}
}
}

public class CopyFileAddLineNumber
{
public static void Main(string[] args)
{
string infname = "CopyFileAddLineNumber.cs";
string outfname = "CopyFileAddLineNumber.txt";
if( args.Length >= 1 ) infname = args[0];
if( args.Length >= 2 ) outfname = args[1];

try
{
FileStream fin = new FileStream(
infname, FileMode.Open, FileAccess.Read );
FileStream fout = new FileStream(
outfname, FileMode.Create, FileAccess.Write );

StreamReader brin = new StreamReader(
fin, System.Text.Encoding.Default );
StreamWriter brout = new StreamWriter(
fout, System.Text.Encoding.Default );

int cnt = 0; // 行号
string s = brin.ReadLine();
while ( s != null )
{
cnt ++;
s = deleteComments(s); //去掉以//开始的注释
brout.WriteLine(cnt + ": \t" + s ); //写出
Console.WriteLine(cnt + ": \t" + s ); //在控制上显示
s = brin.ReadLine(); //读入
}
brin.Close(); // 关闭缓冲读入流及文件读入流的连接.
brout.Close();
}
catch (FileNotFoundException)
{
Console.WriteLine("File not found!" );
}
catch (IOException e2)
{
Console.WriteLine( e2 );
}
}

static string deleteComments( string s ) //去掉以//开始的注释
{
if( s==null ) return s;
int pos = s.IndexOf( "//" );
if( pos<0 ) return s;
return s.Substring( 0, pos );
}
}

Serializble

  • BinaryFormatter
  • XmlFormatter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializationTest
{
[Serializable]
class Book {
public string name;
//public double price;
public int num=13; //如果book的版本变了,反序列化时,这个默认值不会被执行
public string [] reader;

override public string ToString(){
return name + ":" //+ price
+ ":" + string.Join(",",reader)
+ ":" + num;
}
}
class Program
{
static void Main(string[] args)
{

}

static void TestBinary()
{
BinaryFormatter formatter = new BinaryFormatter();

//Serialization of String Object
Book book = new Book();
book.name = "学习C#";
book.price = 123.45;
book.reader = new string[]{ "张三", "李四", "王五"};

FileStream stream = new FileStream("C:\\StrObj.t", FileMode.Create, FileAccess.Write ,
FileShare.None);
formatter.Serialize(stream, book);
stream.Close();

//Deserialization of String Object
FileStream readstream = new FileStream("C:\\StrObj.t", FileMode.Open , FileAccess.Read ,
FileShare.Read );
Book book2 = (Book)formatter.Deserialize(readstream);
readstream.Close();
Console.WriteLine(book2);
Console.ReadLine();
}
}
}

Directory,File

Directory,DirectoryInfo
File,FileInfo

FileSystemInfo 是 DirectoryInfo,FileInfo 的父类

FileInfo 实例信息包含:

  • Name
  • Extension
  • FullName
  • Length
  • CreationTime
  • LastAccessTime
  • LastWriteTime
  • DirectoryName
  • Attributes

DirectoryInfo 实例信息包含:

  • Name
  • FullName
  • CreationTime
  • LastAccessTime
  • LastWriteTime
  • Parent
  • Root

File 新建,复制,移动,删除文件:

  • Create(filePath)
  • Copy(filePath1,filePath2)
  • Move(filePath1,filePath2)
  • Delete(filePath1)
  • Exists(filePath)
  • CreateText(filePath);

Directory 新建,复制,移动,删除文件夹:

  • CreateDirectory(dirPath)
  • Move|Delete|Exists
  • GetDirectories(dirPath)
  • GetFiles(dirPath)

Path

Combine
GetDirectoryName
GetExtension
GetFileName
GetFileNameWithoutExtension
GetFullPath
GetPathRoot
GetTempFileName
GetTempPath

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.IO;
class ListAllFiles
{
public static void Main(string[] args)
{
ListFiles( new DirectoryInfo( @"c:\workspace"));
}

public static void ListFiles( FileSystemInfo info )
{
if( ! info.Exists ) return;

DirectoryInfo dir = info as DirectoryInfo;
if( dir == null ) return; //不是目录

FileSystemInfo [] files = dir.GetFileSystemInfos();
for( int i=0; i<files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if( file != null ) // 是文件
{
Console.WriteLine(
file.FullName + "\t" + file.Length );
}
else //是目录
{
ListFiles( files[i] ); //对于子目录,进行递归调用
}
}
}
}