//
文件流
public void FileStream1()
... {
//将已知硬盘里的文件,写到另一个硬盘里
System.IO.FileStream rfs = new FileStream(@"F: et 练习.88-一.bmp",System.IO.FileMode.Open) ;
//System.IO.FileStream rfs = System.IO.File.Open (@"F: et 练习.88-一.bmp"); // 定义打开只读文件的一种格式
System.IO.FileStream wfs = new FileStream(@"d:8-一.bmp",System.IO.FileMode.Create) ; //@为转义字符,转义后面的
System.IO.FileInfo fi = new System.IO.FileInfo ("");
byte [] ba = new byte [fi.Length]; //定义一个8K的缓存,字符数组
int r = 0 ; //定义一个变量做为存放先取出的数据
byte[] ba = new byte [1024 * 8];
int r = 0;
while ((r = rfs.Read(ba,0,ba.Length)) != 0)
...{
wfs.Write(ba,0,r);
}
rfs.Close () ;
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string nsql = "Insert into land_imagestream (id,imagestream) values(se_imagestream.nextval,:imagestream) ";
OracleCommand ncomm = new OracleCommand(nsql, conn);
ncomm.Parameters.Add("imagestream",OracleType.Blob);
ncomm.Parameters["imagestream"].Value = ba;
ncomm.ExecuteNonQuery();
ncomm.Dispose();
conn.Close();
System.IO.FileStream wfs = new FileStream(@"d: est1.rar",System.IO.FileMode.Create) ; // 建一个目录,将获取的文件拷贝到该目录下
System.Net.WebClient a = new System.Net.WebClient() ;
System.IO.Stream fs = a.OpenRead("http://10.0.3.46/test.rar") ;//从网络上获取的文件
// byte [] ba = new byte[1024 * 8] ;
// int r =0 ;
// while ((r = fs.Read(ba,0,ba.Length)) != 0)
// {
// wfs.Write(ba,0,r) ;
// }
Rw(fs,wfs) ;
fs.Close () ;
wfs.Close () ;
}
private static void Rw(System.IO.Stream a ,System.IO.Stream b)
... {
byte [] ba = new byte [1024 * 8] ;
int r = 0 ;
while ((r = a.Read(ba ,0 ,ba.Length)) != 0)
...{
b.Write(ba,0,r) ;
}
}
public void FileStreams2()
... {
//将汉字传到指定的磁盘文件里
System.IO.FileStream wfs = new FileStream(@"d: est.txt",System.IO.FileMode.Create) ;
string s = "中文汉字" ;
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312") ;
byte [] ba = gb.GetBytes(s);
wfs.Write (ba,0,ba.Length);
wfs.Close() ;
//将磁盘指定路径下的文件输出
System.IO.FileStream rfs = new FileStream(@"d: est.txt",System.IO.FileMode.Open) ;
System.Text.Encoding gb = System.Text.Encoding .GetEncoding("gb2312") ;
// byte [] ba = new byte[1024] ;
// string s = string.Empty ;
System.IO.StreamReader sw = new StreamReader(rfs,gb) ;
string s =sw.ReadToEnd();
// int r = 0 ;
// while ((r = rfs.Read(ba,0,ba.Length))!= 0 )
// {
// s = s+gb.GetString(ba,0,r) ;
// }
rfs.Close () ;
System.Console.WriteLine (s) ;
System.Console.ReadLine() ;
}
public void FileStream3()
... {
// 使用StreamReader
System.IO.FileStream rfs = new System.IO.FileStream (@"d:aaa.txt",System.IO.FileMode.Open) ;
System.IO.FileStream wfs = new System.IO.FileStream (@"f:新建文件.txt",System.IO.FileMode.Create) ;
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312") ;
// byte [] ba = new byte [1024 * 8] ;
//
// int r = 0 ;
//
// while ((r = rfs.Read (ba,0,ba.Length)) != 0 )
// {
// wfs.Write(ba,0,r) ;
// }
System.IO.StreamReader sr = new System.IO.StreamReader (rfs,gb) ;
string s = sr.ReadToEnd() ;
System.IO.StreamWriter sw = new System.IO.StreamWriter(wfs,gb) ;
sw.Write(s) ;
sr.Close() ;
sw.Close() ;
rfs.Close() ;
wfs.Close() ;
System.Console.Write(s) ;
System.Console.Read() ;
}
}
public void FileStream1()
... {
//将已知硬盘里的文件,写到另一个硬盘里
System.IO.FileStream rfs = new FileStream(@"F: et 练习.88-一.bmp",System.IO.FileMode.Open) ;
//System.IO.FileStream rfs = System.IO.File.Open (@"F: et 练习.88-一.bmp"); // 定义打开只读文件的一种格式
System.IO.FileStream wfs = new FileStream(@"d:8-一.bmp",System.IO.FileMode.Create) ; //@为转义字符,转义后面的
System.IO.FileInfo fi = new System.IO.FileInfo ("");
byte [] ba = new byte [fi.Length]; //定义一个8K的缓存,字符数组
int r = 0 ; //定义一个变量做为存放先取出的数据
byte[] ba = new byte [1024 * 8];
int r = 0;
while ((r = rfs.Read(ba,0,ba.Length)) != 0)
...{
wfs.Write(ba,0,r);
}
rfs.Close () ;
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string nsql = "Insert into land_imagestream (id,imagestream) values(se_imagestream.nextval,:imagestream) ";
OracleCommand ncomm = new OracleCommand(nsql, conn);
ncomm.Parameters.Add("imagestream",OracleType.Blob);
ncomm.Parameters["imagestream"].Value = ba;
ncomm.ExecuteNonQuery();
ncomm.Dispose();
conn.Close();
System.IO.FileStream wfs = new FileStream(@"d: est1.rar",System.IO.FileMode.Create) ; // 建一个目录,将获取的文件拷贝到该目录下
System.Net.WebClient a = new System.Net.WebClient() ;
System.IO.Stream fs = a.OpenRead("http://10.0.3.46/test.rar") ;//从网络上获取的文件
// byte [] ba = new byte[1024 * 8] ;
// int r =0 ;
// while ((r = fs.Read(ba,0,ba.Length)) != 0)
// {
// wfs.Write(ba,0,r) ;
// }
Rw(fs,wfs) ;
fs.Close () ;
wfs.Close () ;
}
private static void Rw(System.IO.Stream a ,System.IO.Stream b)
... {
byte [] ba = new byte [1024 * 8] ;
int r = 0 ;
while ((r = a.Read(ba ,0 ,ba.Length)) != 0)
...{
b.Write(ba,0,r) ;
}
}
public void FileStreams2()
... {
//将汉字传到指定的磁盘文件里
System.IO.FileStream wfs = new FileStream(@"d: est.txt",System.IO.FileMode.Create) ;
string s = "中文汉字" ;
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312") ;
byte [] ba = gb.GetBytes(s);
wfs.Write (ba,0,ba.Length);
wfs.Close() ;
//将磁盘指定路径下的文件输出
System.IO.FileStream rfs = new FileStream(@"d: est.txt",System.IO.FileMode.Open) ;
System.Text.Encoding gb = System.Text.Encoding .GetEncoding("gb2312") ;
// byte [] ba = new byte[1024] ;
// string s = string.Empty ;
System.IO.StreamReader sw = new StreamReader(rfs,gb) ;
string s =sw.ReadToEnd();
// int r = 0 ;
// while ((r = rfs.Read(ba,0,ba.Length))!= 0 )
// {
// s = s+gb.GetString(ba,0,r) ;
// }
rfs.Close () ;
System.Console.WriteLine (s) ;
System.Console.ReadLine() ;
}
public void FileStream3()
... {
// 使用StreamReader
System.IO.FileStream rfs = new System.IO.FileStream (@"d:aaa.txt",System.IO.FileMode.Open) ;
System.IO.FileStream wfs = new System.IO.FileStream (@"f:新建文件.txt",System.IO.FileMode.Create) ;
System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312") ;
// byte [] ba = new byte [1024 * 8] ;
//
// int r = 0 ;
//
// while ((r = rfs.Read (ba,0,ba.Length)) != 0 )
// {
// wfs.Write(ba,0,r) ;
// }
System.IO.StreamReader sr = new System.IO.StreamReader (rfs,gb) ;
string s = sr.ReadToEnd() ;
System.IO.StreamWriter sw = new System.IO.StreamWriter(wfs,gb) ;
sw.Write(s) ;
sr.Close() ;
sw.Close() ;
rfs.Close() ;
wfs.Close() ;
System.Console.Write(s) ;
System.Console.Read() ;
}
}