Java输入输出流方法

1,Java中操作方法:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;    
  2. public class FileInputStreamTest    
  3. {    
  4.     public static void main(String[] args) throws IOException    
  5.     {    
  6.         //创建字节输入流    
  7.         FileInputStream fis = new FileInputStream("FileInputStreamTest.java");    
  8.         //创建一个长度为1024的竹筒    
  9.         byte[] bbuf = new byte[1024];    
  10.         //用于保存实际读取的字节数    
  11.         int hasRead = 0;    
  12.         //使用循环来重复“取水”的过程    
  13.         while((hasRead = fis.read(bbuf))>0)    
  14.         {    
  15.             //取出"竹筒"中(字节),将字节数组转成字符串输入    
  16.             System.out.println(new String(bbuf,0,hasRead));    
  17.         }    
  18.         fis.close();    
  19.     }    
  20. }    

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;    
  2. public class FileReaderTest    
  3. {    
  4.     public static void main(String[] args) throws IOException     
  5.     {    
  6.         FileReader fr = null;    
  7.         try    
  8.         {    
  9.             //创建字符输入流    
  10.             fr = new FileReader("FileReaderTest.java");    
  11.             //创建一个长度为32的"竹筒"    
  12.             char[] cbuf = new char[32];    
  13.             //用于保存实际读取的字符数    
  14.             int hasRead = 0;    
  15.             //使用循环来重复“取水”的过程    
  16.             while((hasRead = fr.read(cbuf))>0)    
  17.             {    
  18.                 //取出"竹筒"中(字节),将字节数组转成字符串输入    
  19.                 System.out.println(new String(cbuf,0,hasRead));    
  20.             }    
  21.         }    
  22.         catch (IOException ioe)    
  23.         {    
  24.             ioe.printStackTrace();    
  25.         }    
  26.         finally    
  27.         {    
  28.             //关闭文件输入流    
  29.             if(fr != null)    
  30.             {    
  31.                 fr.close();    
  32.             }    
  33.         }    
  34.     }    
  35. }      

2,C#中操作方法:

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /* - - - - - - - - - - - - - - - - - - - - - - - -   
  2.  * Stream 和 byte[] 之间的转换  
  3.  * - - - - - - - - - - - - - - - - - - - - - - - */   
  4. /// <summary>   
  5. /// 将 Stream 转成 byte[]   
  6. /// </summary>   
  7. public byte[] StreamToBytes(Stream stream)   
  8. {   
  9.     byte[] bytes = new byte[stream.Length];   
  10.     stream.Read(bytes, 0, bytes.Length);   
  11.     // 设置当前流的位置为流的开始   
  12.     stream.Seek(0, SeekOrigin.Begin);   
  13.     return bytes;   
  14. }   
  15. /// <summary>   
  16. /// 将 byte[] 转成 Stream   
  17. /// </summary>   
  18. public Stream BytesToStream(byte[] bytes)   
  19. {   
  20.     Stream stream = new MemoryStream(bytes);   
  21.     return stream;   
  22. }   
  23.   
  24. /* - - - - - - - - - - - - - - - - - - - - - - - -   
  25.  * Stream 和 文件之间的转换  
  26.  * - - - - - - - - - - - - - - - - - - - - - - - */   
  27. /// <summary>   
  28. /// 将 Stream 写入文件   
  29. /// </summary>   
  30. public void StreamToFile(Stream stream,string fileName)   
  31. {   
  32.     // 把 Stream 转换成 byte[]   
  33.     byte[] bytes = new byte[stream.Length];   
  34.     stream.Read(bytes, 0, bytes.Length);   
  35.     // 设置当前流的位置为流的开始   
  36.     stream.Seek(0, SeekOrigin.Begin);   
  37.     // 把 byte[] 写入文件   
  38.     FileStream fs = new FileStream(fileName, FileMode.Create);   
  39.     BinaryWriter bw = new BinaryWriter(fs);   
  40.     bw.Write(bytes);   
  41.     bw.Close();   
  42.     fs.Close();   
  43. }   
  44. /// <summary>   
  45. /// 从文件读取 Stream   
  46. /// </summary>   
  47. public Stream FileToStream(string fileName)   
  48. {               
  49.     // 打开文件   
  50.     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);   
  51.     // 读取文件的 byte[]   
  52.     byte[] bytes = new byte[fileStream.Length];   
  53.     fileStream.Read(bytes, 0, bytes.Length);   
  54.     fileStream.Close();   
  55.     // 把 byte[] 转换成 Stream   
  56.     Stream stream = new MemoryStream(bytes);   
  57.     return stream;   
  58. }   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值