10.9文件流

//  文件流
         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() ;
        }

    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值