• 压缩和解压,文件读取练习


    压缩

    先读取,再写入。只是在写入的时候,需要根据规则写入,如果用用压缩流写入,就成了压缩。

     1  using (FileStream fs = new FileStream("yasuo.txt", FileMode.Open, FileAccess.Read))
     2             {
     3                 byte[] buffer = new byte[1024 * 1024 * 5];
     4                 int readCount = fs.Read(buffer, 0, buffer.Length);
     5                 using (FileStream fWrite = new FileStream("newYaSuo.txt", FileMode.Create, FileAccess.Write))
     6                 {
     7                     using (GZipStream zip = new GZipStream(fWrite, CompressionMode.Compress))
     8                     {
     9                         while (readCount > 0)
    10                         {
    11                             zip.Write(buffer, 0, readCount);
    12                             readCount = fs.Read(buffer, 0, buffer.Length);
    13                         }
    14                     }
    15 
    16                 }
    17             }
    View Code

    解压

    解压前,先要读取,然后再写入。只是开始读取后,如果直接写入,写入的还是以前压缩的东西,所以在写入前先要解压。 

     1 using (FileStream fs = new FileStream("newYaSuo.txt", FileMode.Open, FileAccess.Read))
     2             {
     3                 byte[] buffer = new byte[1024 * 1024 * 5];
     4                 using (GZipStream zip = new GZipStream(fs, CompressionMode.Decompress))
     5                 {
     6                     int readCount = zip.Read(buffer, 0, buffer.Length);
     7                     using (FileStream fWrite = new FileStream("newJieYaSuo.txt", FileMode.Create, FileAccess.Write))
     8                     {
     9 
    10                         while (readCount > 0)
    11                         {
    12                             fWrite.Write(buffer, 0, readCount);
    13                             readCount = zip.Read(buffer, 0, buffer.Length);
    14                         }
    15 
    16                     }
    17                 }
    18             }
    View Code

    文件读取练习:人员信息维护

     1 public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7 
     8         private void Form1_Load(object sender, EventArgs e)
     9         {
    10             BindListBox();
    11 
    12         }
    13 
    14         private void BindListBox()
    15         {
    16             List<Person> list = new List<Person>();
    17             string line = string.Empty;
    18             
    19             //读取文件中的数据
    20             using (StreamReader sRead = new StreamReader("person.txt", Encoding.Default))
    21             {
    22                 while ((line=sRead.ReadLine())!=null)
    23                 {
    24                     string[] strs = line.Split(',');
    25                     Person p = new Person();
    26                     p.Name = strs[0];
    27                     p.Age = Convert.ToInt32(strs[1]);
    28                     list.Add(p);
    29                 }
    30             }
    31 
    32             lbPersonList.DataSource = list;
    33             lbPersonList.DisplayMember = "Name";
    34             lbPersonList.ValueMember = "Age";
    35         }
    36 
    37         private void btnAdd_Click(object sender, EventArgs e)
    38         {
    39             using (StreamWriter sWrite=new StreamWriter("person.txt", true,Encoding.Default))
    40             {
    41                 sWrite.WriteLine(string.Format("{0},{1}",txtName.Text.Trim(),txtAge.Text.Trim()));
    42                 //lbPersonList.Items.Add(txtName.Text.Trim());
    43                 
    44             }
    45             BindListBox();
    46         }
    47 
    48         private void lbPersonList_SelectedIndexChanged(object sender, EventArgs e)
    49         {
    50             Person p = lbPersonList.SelectedItem as Person;
    51             if (p!=null)
    52             {
    53                 txtName.Text = p.Name;
    54                 txtAge.Text = p.Age.ToString();
    55             }
    56         }
    57 
    58         private void btnClear_Click(object sender, EventArgs e)
    59         {
    60             txtAge.Text = "";
    61             txtName.Text = "";
    62         }
    63     }
    64 
    65     public class Person
    66     {
    67         public string Name { get; set; }
    68 
    69         public int Age { get; set; }
    70     }
    View Code
  • 相关阅读:
    洛谷1052——过河(DP+状态压缩)
    mod4最优路径问题(转载)
    初次接触python,怎么样系统的自学呢?
    对AM信号FFT的matlab仿真
    初次使用自己写的testbench 验证了简单的NOT门。
    写简单的tb(testbench)文件来测试之前的FSM控制的LED
    利用简单的有限状态机(FSM)来实现一个简单的LED流水灯
    利用状态机(FSM)进行简单的uart串口发送数据
    用matlab脚本语言写M文件函数时用三种方法简单实现实现DFT(离散傅里叶变换)
    作为新手对于初次接触matlab的一些感受。
  • 原文地址:https://www.cnblogs.com/wesley168/p/6636333.html
Copyright © 2020-2023  润新知