File and Directory

// ———-TEST THE BINARY READER AND WRITTER——————
BinaryWriter writer = new BinaryWriter(File.Create(“D:/Test/test01.bin”));
Byte b = 45;
string message = “Hello, How are you?”;
//——–Ghi du lieu kieu Byte vao tep—–
writer.Write(b);
//——-Ghi du lieu string vao tep———
writer.Write(message);
writer.Flush();
writer.Close();
//—————–READ DATA FROM FILE————-
BinaryReader reader = new BinaryReader(File.OpenRead(“D:/Test/test01.bin”));
Byte b1 = reader.ReadByte();
string msg = reader.ReadString();
Console.WriteLine(b1);
Console.WriteLine(msg);
reader.Close();

// ———–TEST MEMORY TREAM————–
MemoryStream memStr = new MemoryStream();
BufferedStream buffStr = new BufferedStream(memStr);
buffStr.WriteByte((byte)100);
buffStr.Position = 0;
byte[] arrByte = {1,2,3 };

buffStr.Read(arrByte, 1, 2);

for (int i = 0; i < 3; i++)
{
Console.WriteLine(“The value is: {0}”,arrByte[i]);
}
buffStr.Position = 0;
Console.WriteLine(“The value of stream is: {0}”, buffStr.ReadByte());
//———–Read data from buuffer to FIle————-

//——TEST THE TEXT READER————————
string file;
Console.WriteLine(“GO ten tep can doc:”);
file = Console.ReadLine();
if (!File.Exists(file))
{
Console.WriteLine(“File does not exist.”);
return;
}
StreamReader reader = File.OpenText(file);
string outPut=””;
while ((outPut = reader.ReadLine()) != null)
{
Console.WriteLine(outPut);
}
Console.WriteLine(“The end of file”);
reader.Close();
// ———TEST TEXT WRITER———————-

string input;
StreamWriter writer;
Console.WriteLine(“Enter file name:”);
file = Console.ReadLine();

if (!File.Exists(file))
{
writer = File.CreateText(file);
}
else
{
writer = File.AppendText(file);
}
Console.WriteLine(“Enter text to save:”);
input = Console.ReadLine();
writer.WriteLine(input);
writer.Close();
//————–TEST THE FILE CLASS———–

string file = @”D:\TEST\TEST03.txt”;
FileStream fs = File.Create(file);
Byte[] info = new UTF8Encoding(true).GetBytes(“TÔI YÊU VIỆT NAM, VIỆT NAM RẤT ĐẸP, ĐẸP GHÊ NGƯỜI”);
fs.Write(info, 0, info.Length);
fs.Flush();
fs.Close();
Console.WriteLine(“File da duoc tao lap”);
//———-COPY FILE METHOD————
string Source_file = @”D:\TEST\TEST03.txt”;
string Des_file = @”D:\TEST\TEST04.txt”;
// File.Delete(Des_file);
FileInfo objInfo = new FileInfo(Source_file);
Console.WriteLine(“The tim created: {0}”,objInfo.CreationTime.ToString());
//——————-TEST THE DIRECTORY ————————
string path = @”D:\TEST\TESTDIR”;
DirectoryInfo dirInfo = Directory.CreateDirectory(path);
Console.WriteLine(“{0} was created successful, The time created is {1}”, path, dirInfo.CreationTime.ToString());
string file = path + “\\test.txt”;
FileStream stream = File.Create(file);
Byte[] info = new UTF8Encoding(true).GetBytes(“JKHSDFKJSDHFUEWIRHW”);
stream.Write(info,0,info.Length);
stream.Flush();
stream.Close();
//—————–TEST THE METHOD OF DIRECTORY———–
string path = @”D:\TEST”;
string[] dirName = Directory.GetDirectories(path);
string[] fileName = Directory.GetFiles(path);
for (int i = 0; i < fileName.Length; i++)
{
Console.WriteLine(fileName[i]);
}

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s