zoukankan      html  css  js  c++  java
  • 文件和流的相互转化

     1 public class UploadFile
     2 {
     3     //文件名称
     4     public string FileName{get;set;}
     5     //文件类型
     6     public StorageFile File{get;set;}
     7     
     8     //文件数据流
     9    public Stream FileStream { get; private set; }
    10    
    11    
    12     public UploadFile(StorageFile file)
    13     {
    14         this.StorageFile=file.Name;
    15         this.File=file;
    16     }
    17     
    18     //将文件转换成流
    19     public async Task<byte[]> GetBytes()
    20     {
    21         FileStream =(Stream) await File.OpenStreamForReadAsync();
    22         byte[] bytes = new byte[FileStream.Length];
    23         FileStream.Read(bytes, 0, bytes.Length);
    24         FileStream.Seek(0, SeekOrigin.Begin);
    25         return bytes;
    26     }
    27     
    28     
    29     
    30     //将流写入文件
    31        public async void GetFile(Byte[] bytes)
    32         { 
    33             StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
    34             StorageFile storageFile = await applicationFolder.CreateFileAsync("1.txt", CreationCollisionOption.ReplaceExisting);
    35             StorageFile file = await applicationFolder.GetFileAsync("1.txt");
    36             using (var stream = await file.OpenStreamForWriteAsync())
    37             {
    38                 await stream.WriteAsync(bytes, 0, bytes.Length);
    39                 stream.Flush();
    40             }
    41 
    42     
    43 }
  • 相关阅读:
    poj 1584
    poj 1113 & poj 2187
    pku 1321 棋盘问题
    poj 1408
    pku 2251 Dungeon Master
    sdut oj 2218 Give Me an E
    Android工程 单元测试
    Android Timer编写方式
    去除工程的.svn隐藏文件夹
    Android 绑定远程服务出现 Not Allowed to bind service
  • 原文地址:https://www.cnblogs.com/lj940306/p/4681140.html
Copyright © 2011-2022 走看看