Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

6/3/2014

1
Input/Output with
.NET Framework Class Library
Phm Minh Tun
B mn CNPM Khoa CNTT
Ni dung trnh by
Navigating the File System
Reading and Writing Files
Compressing Streams
Navigating the File System
Nhu cu:
Lm sao bit c trn h thng c nhng
a no?
Lm sao ly c danh sch tp tin v th mc
con ca mt th mc no ?
Lm sao truy xut c cc thuc tnh ca mt
tp tin, th mc?
Lm sao gim st c s thay i ca mt tp
tin, th mc?

Navigating the File System
Cc lp h tr
DriveInfo class
DirectoryInfo class
FileInfo class
Path class
FileSystemWatcher class
File class
Directory class
6/3/2014
2
Navigating the File System
DriveInfo class
Thuc tnh
Navigating the File System
DriveInfo class
DriveType enum
Phng thc
Navigating the File System
FileSystemInfo class
Thuc tnh
Navigating the File System
FileSystemInfo class
Phng thc
6/3/2014
3
Navigating the File System
DirectoryInfo class
Thuc tnh
Navigating the File System
DirectoryInfo class
Phng thc
Navigating the File System
FileInfo class
Thuc tnh
Navigating the File System
FileInfo class
Phng thc
6/3/2014
4
Navigating the File System
Path class
Phng thc
Navigating the File System
Path class
Phng thc
Navigating the File System
FileSystemWatcher class
Thuc tnh
Navigating the File System
FileSystemWatcher class
Phng thc
S kin
6/3/2014
5
Navigating the File System
Lm sao bit c trn h thng c nhng a
no?
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Console.WriteLine("Drive: {0}", drive.Name);
Console.WriteLine("Type: {0}",
drive.DriveType);
}
Navigating the File System
Lm sao ly c danh sch tp tin v th mc con
ca mt th mc no ?
DirectoryInfo ourDir = new
DirectoryInfo(@"c:\windows");
Console.WriteLine("Directory: {0}", ourDir.FullName);
foreach (FileInfo file in ourDir.GetFiles())
{
Console.WriteLine("File: {0}", file.Name);
}
Navigating the File System
Lm sao truy xut c cc thuc tnh ca mt
tp tin, th mc?
FileInfo ourFile = new FileInfo(@"c:\boot.ini ");
if (ourFile.Exists)
{
Console.WriteLine("Filename : {0}",
ourFile.Name);
Console.WriteLine("Path : {0}",
ourFile.FullName);
}
Navigating the File System
Lm sao gim st c s thay i ca mt tp tin, th mc?
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"c:\";
watcher.Renamed += new
RenamedEventHandler(watcher_Renamed);
watcher.EnableRaisingEvents = true;
static void watcher_Renamed(object sender,
RenamedEventArgs e)
{
Console.WriteLine("Renamed from {0} to {1}",
e.OldFullPath, e.FullPath);
}
6/3/2014
6
Navigating the File System
File class
Cung cp tt c cc phng thc cn thit
cho vic to file, xa file, di chuyn file,
sao chp file
Directory class
Cung cp tt c cc phng thc cn thit
cho vic to th mc, xa th mc, di
chuyn th mc, ly danh sch tp tin, th
mc con
Reading and Writing Files
Cc lp h tr
Stream class
FileStream class
StreamReader class, StreamWriter class
BinaryReader class, BinaryWriter class
MemoryStream class
BufferedStream class
Reading and Writing Files
Stream class
Thuc tnh
Reading and Writing Files
Stream class
Phng thc
6/3/2014
7
Reading and Writing Files
FileStream class: k tha Stream class
Thuc tnh
Phng thc
Reading and Writing Files
StreamReader class
K tha TextReader class
Dng c cc file vn bn
Thuc tnh
Reading and Writing Files
StreamReader class
Phng thc
Reading and Writing Files
V d
c file vn bn
FileStream theFile = File.Open(@"C:\boot.ini",
FileMode.Open, FileAccess.Read);
StreamReader rdr = new StreamReader(theFile);
while (!rdr.EndOfStream)
{
string line = rdr.ReadLine();
Console.WriteLine(line);
}
rdr.Close();
theFile.Close();
6/3/2014
8
Reading and Writing Files
StreamWriter class
K tha TextWriter class
Dng ghi cc file vn bn
Thuc tnh
Reading and Writing Files
StreamWriter class
Phng thc
Reading and Writing Files
V d
Ghi file vn bn
FileStream theFile = File.Open(@"c:\somefile.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(theFile);
writer.WriteLine("Hello");
writer.Close();
theFile.Close();
Reading and Writing Files
BinaryReader class
Dng c file nh phn
Cung cp cc phng thc c dng
ReadXXX(ReadByte, ReadInt32) c
ni dung file.
6/3/2014
9
Reading and Writing Files
V d
c file nh phn
FileStream theFile = File.Open(@"c:\somefile.bin",
FileMode.Open);
BinaryReader reader = new
BinaryReader(theFile);
long number = reader.ReadInt64();
byte[] bytes = reader.ReadBytes(4);
string s = reader.ReadString();
Reading and Writing Files
BinaryWriter class
Dng ghi file nh phn
Cung cp cc phng thc Write vi nhiu
dng tham s khc nhau ghi ni dung
file.
Reading and Writing Files
V d
Ghi file nh phn
FileStream theFile = File.Open(@"c:\somefile.bin",
FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter writer = new
BinaryWriter(theFile);
long number = 100;
byte[] bytes = new byte[] { 10, 20, 50, 100 };
string s = Toi di hoc";
writer.Write(number);
writer.Write(bytes);
writer.Write(s);
Reading and Writing Files
MemoryStream class: k tha Stream class
Thuc tnh
Phng thc
6/3/2014
10
Reading and Writing Files
V d
MemoryStream memStrm = new MemoryStream();
StreamWriter writer = new StreamWriter(memStrm);
writer.WriteLine("Hello");
writer.WriteLine("Goodbye");
writer.Flush();
FileStream theFile =
File.Create(@"c:\inmemory.txt");
memStrm.WriteTo(theFile);
Reading and Writing Files
BufferedStream class
Thng dng cho NetworkStream
Tng hiu qu c/ghi d liu
FileStream newFile =
File.Create(@"c:\test.txt");
BufferedStream buffered =
new BufferedStream(newFile);
StreamWriter writer =
new StreamWriter(buffered);
writer.WriteLine("Some data");
Compressing Streams
Cho php nn d liu gc ti a l 4GB
Kiu nn: GZIP v DEFLATE
Cc lp h tr
GZipStream class
DeflateStream class
Compressing Streams
GZipStream class
K tha Stream class
Dng nn/gii nn tp tin theo GZIP
Nn tp tin
FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);
GZipStream compStream = new GZipStream(sourceFile,
CompressionMode.Compress);
byte[] Arr = new byte[4096];
int theByte = sourceFile.Read(Arr, 0, 4096);
while(theByte!=0)
{
compStream.Write(Arr, 0, theByte);
theByte = sourceFile.Read(Arr, 0, 4096);
}
6/3/2014
11
Compressing Streams
GZipStream class
Gii nn tp tin
FileStream sourceFile = File.OpenRead(inFilename);
FileStream destFile = File.Create(outFilename);
GZipStream compStream = new
GZipStream(sourceFile,
CompressionMode.Decompress);
byte[] Arr = new byte[4096];
int theByte = compStream.Read(Arr, 0, 4096);
while (theByte != 0)
{
destFile.Write(Arr, 0, theByte);
theByte = compStream.Read(Arr, 0, 4096);
}
Compressing Streams
DeflateStream class
Kt hp gia LZ77 v Huffman
Dng tng t nh GZipStream class
Cho kch thc nn nh hn
File nn khng th m c bng cc
chng trnh gii nn khc.
Q&A
Cm n cc bn theo di!
6/3/2014
12

You might also like