主要内容与代码来自《C#入门经典》
四、DirectoryInfo类
DirectoryInfo类的作用类似于FileInfo类。它是一个实例化的对象,表示计算机上的单一目录。
1)如果进行单一调用,就使用静态Directory类。
2)如果进行一系列调用,则使用实例化的Directory对象。
五、FileStream对象
FileStream对象表示在磁盘或网络路径上指向文件的流。该类提供了在文件中读写字节
的方法,其操作的是字节(raw byte),而Stream类(StreamReader/StreamWriter)操
作的是字符数据。
static void Main(string[] args)
{
byte[] byData = new byte[200];
char[] charData = new char[200];
try
{
FileStream fs = new FileStream("test.txt", FileMode.Open);
fs.Seek(0, SeekOrigin.Begin);
fs.Read(byData, 0, 200);
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadKey();
return;
}
Decoder d = Encoding.UTF8.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
Console.WriteLine(charData);
Console.ReadKey();
}
static void Main(string[] args)
{
byte[] byData;
char[] charData;
try
{
FileStream fs = new FileStream("tmp.txt", FileMode.Create);
charData = "Things can only get better! 沉默的时光!".ToCharArray();
byData = new byte[charData.Length*2];
Encoder e = Encoding.UTF8.GetEncoder();
e.GetBytes(charData, 0, charData.Length, byData, 0, true);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(byData, 0, byData.Length);
fs.Flush();
fs.Close();
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadKey();
return;
}
六、StreamWriter/StreamReader
StreamWriter类允许将字符或字符串写入到文件中,它处理底层的转换,向FileStream对象写入数据。
static void Main(string[] args)
{
try
{
//FileStream fs = new FileStream("tmp.txt", FileMode.OpenOrCreate);
//StreamWriter sw = new StreamWriter(fs);
StreamWriter sw = new StreamWriter("tmp.txt");
bool truth = true;
sw.WriteLine("Hello, world.");
sw.WriteLine("It is now {0} and things are looking good.",
DateTime.Now.ToLongDateString());
sw.Write("More than that,");
sw.Write(" it's {0} that C# is fun.", truth);
sw.Close();
StreamReader sr = new StreamReader("tmp.txt");
// 1)使用ReadLine读取数据
string strLine;
strLine = sr.ReadLine();
while (strLine != null)
{
Console.WriteLine(strLine);
strLine = sr.ReadLine();
}
sr.Close();
/* 2)使用ReadLine读取数据
int nChar;
nChar = sr.Read();
while (nChar != -1)
{
Console.Write(Convert.ToChar(nChar));
nChar = sr.Read();
}
sr.Close();
3)使用ReadToEnd读取数据
string strLine;
strLine = sr.ReadToEnd();
Console.WriteLine(strLine);
sr.Close();
4)使用File.ReadLines读取数据
foreach (string strLine in File.ReadLines("Log.txt"))
{
Console.WriteLine(strLine);
}
*/
Console.ReadKey();
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadKey();
return;
}
}
class Program
{
private static List<Dictionary<string, string>> GetData(
out List<string> columns)
{
string line;
string[] stringArray;
char[] charArray = new char[] { ',' };
List<Dictionary<string, string>> data =
new List<Dictionary<string, string>>();
columns = new List<string>();
try
{
FileStream fs = new FileStream("tmp.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
// Obtain the columns from the first line.
// Split row of data into string array
line = sr.ReadLine();
stringArray = line.Split(charArray);
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
columns.Add(stringArray[x]);
}
line = sr.ReadLine();
while (line != null)
{
stringArray = line.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string, string>();
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
dataRow.Add(columns[x], stringArray[x]);
}
data.Add(dataRow);
line = sr.ReadLine();
}
sr.Close();
return data;
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadKey();
return data;
}
}
static void Main(string[] args)
{
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);
foreach (string column in columns)
{
Console.Write("{0, -20}", column);
}
Console.WriteLine();
foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0, -20}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
http://www.cnblogs.com/itech/archive/2009/08/31/1555835.html
::set source=.\test\
set zipfilename=7za920.zip
set dest=.\test
echo start ziping
7za.exe e %zipfilename% -o%dest%
pause